Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Class unique_any

boost::anys::unique_any — A class whose instances can hold instances of any type (including non-copyable and non-movable types).

Synopsis

// In header: <boost/any/unique_any.hpp>


class unique_any {
public:
  // construct/copy/destruct
  unique_any();
  unique_any(unique_any &&);
  template<typename T> 
    unique_any(T &&, 
               typename std::enable_if<!std::is_same< T &&, boost::any && >::value >::type * = nullptr);
  template<typename BoostAny> 
    unique_any(BoostAny &&, 
               typename std::enable_if< std::is_same< BoostAny &&, boost::any && >::value >::type * = nullptr) noexcept;
  template<typename T, class... Args> 
    explicit unique_any(in_place_type_t< T >, Args &&...);
  template<typename T, typename U, class... Args> 
    explicit unique_any(in_place_type_t< T >, std::initializer_list< U >, 
                        Args &&...);
  unique_any & operator=(unique_any &&);
  template<typename T> unique_any & operator=(T &&);
  ~unique_any();

  // public member functions
  template<typename T, class... Args> 
    std::decay< T >::type & emplace(Args &&...);
  template<typename T, typename U, class... Args> 
    std::decay< T >::type & emplace(std::initializer_list< U >, Args &&...);
  void reset() noexcept;
  void swap(unique_any &) noexcept;
  bool has_value() const noexcept;
  const boost::typeindex::type_info & type() const noexcept;
};

Description

unique_any public construct/copy/destruct

  1. unique_any();

    Postconditions:

    this->has_value() is false.

  2. unique_any(unique_any && other);

    Move constructor that moves content of other into new instance and leaves other empty.

    Postconditions:

    other->has_value() is false.

    Throws:

    Nothing.
  3. template<typename T> 
      unique_any(T && value, 
                 typename std::enable_if<!std::is_same< T &&, boost::any && >::value >::type * = nullptr);

    Forwards value, so that the content of the new instance has type std::decay_t<T> and value is the value before the forward.

    Throws:

    std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type.
  4. template<typename BoostAny> 
      unique_any(BoostAny && value, 
                 typename std::enable_if< std::is_same< BoostAny &&, boost::any && >::value >::type * = nullptr) noexcept;

    Moves the content of boost::any into *this.

    Postconditions:

    value.empty() is true.

    Throws:

    Nothing.
  5. template<typename T, class... Args> 
      explicit unique_any(in_place_type_t< T >, Args &&... args);

    Inplace constructs T from forwarded args..., so that the content of *this is equivalent in type to std::decay_t<T>.

    Throws:

    std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type.
  6. template<typename T, typename U, class... Args> 
      explicit unique_any(in_place_type_t< T >, std::initializer_list< U > il, 
                          Args &&... args);

    Inplace constructs T from li and forwarded args..., so that the initial content of *this is equivalent in type to std::decay_t<T>.

    Throws:

    std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type.
  7. unique_any & operator=(unique_any && rhs);

    Moves content of rhs into current instance, discarding previous content, so that the new content is equivalent in both type and value to the content of rhs before move, or empty if rhs.empty().

    Postconditions:

    rhs->empty() is true

    Throws:

    Nothing.
  8. template<typename T> unique_any & operator=(T && rhs);

    Forwards rhs, discarding previous content, so that the new content of is equivalent in both type and value to rhs before forward.

    Throws:

    std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type. Assignment satisfies the strong guarantee of exception safety.
  9. ~unique_any();

    Releases any and all resources used in management of instance.

    Throws:

    Nothing.

unique_any public member functions

  1. template<typename T, class... Args> 
      std::decay< T >::type & emplace(Args &&... args);

    Inplace constructs T from forwarded args..., discarding previous content, so that the content of *this is equivalent in type to std::decay_t<T>.

    Returns:

    reference to the content of *this.

    Throws:

    std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type.
  2. template<typename T, typename U, class... Args> 
      std::decay< T >::type & 
      emplace(std::initializer_list< U > il, Args &&... args);

    Inplace constructs T from li and forwarded args..., discarding previous content, so that the content of *this is equivalent in type to std::decay_t<T>.

    Returns:

    reference to the content of *this.

    Throws:

    std::bad_alloc or any exceptions arising from the move or copy constructor of the contained type.
  3. void reset() noexcept;

    Postconditions:

    this->has_value() is false.

  4. void swap(unique_any & rhs) noexcept;

    Exchange of the contents of *this and rhs.

    Returns:

    *this

    Throws:

    Nothing.
  5. bool has_value() const noexcept;

    Returns:

    true if instance is not empty, otherwise false.

    Throws:

    Nothing.
  6. const boost::typeindex::type_info & type() const noexcept;

    Useful for querying against types known either at compile time or only at runtime.

    Returns:

    the typeid of the contained value if instance is non-empty, otherwise typeid(void).


PrevUpHomeNext