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 template basic_any

boost::anys::basic_any — A class with customizable Small Object Optimization whose instances can hold instances of any type that satisfies ValueType requirements. Use boost::any instead if not sure.

Synopsis

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

template<std::size_t OptimizeForSize, std::size_t OptimizeForAlignment> 
class basic_any {
public:
  // construct/copy/destruct
  basic_any() noexcept;
  template<typename ValueType> basic_any(const ValueType &);
  basic_any(const basic_any &);
  basic_any(basic_any &&) noexcept;
  template<typename ValueType> 
    basic_any(ValueType &&, 
              typename boost::disable_if< boost::is_same< basic_any &, ValueType > >::type * = 0, 
              typename boost::disable_if< boost::is_const< ValueType > >::type * = 0);
  basic_any & operator=(const basic_any &);
  basic_any & operator=(basic_any &&) noexcept;
  template<typename ValueType> basic_any & operator=(ValueType &&);
  ~basic_any();

  // public member functions
  basic_any & swap(basic_any &) noexcept;
  bool empty() const noexcept;
  void clear() noexcept;
  const boost::typeindex::type_info & type() const noexcept;

  // public data members
  static constexpr std::size_t buffer_size;
  static constexpr std::size_t buffer_align;
};

Description

boost::anys::basic_any is the drop-in replacement for boost::any that provides control over Small Object Optimization via OptimizeForSize and OptimizeForAlignment template parameters.

There are certain applications that require boost::any functionality, do know the typical/maximal size of the stored object and wish to avoid dynamic memory allocation overhead. For the convenience such applications may create a typedef for boost::anys::basic_any with the OptimizeForSize and OptimizeForAlignment template parameters set to typical/maximal size and alignment of types respectively. Memory allocation would be avoided for storing nothrow move constructible types with size and alignment less than or equal to the OptimizeForSize and OptimizeForAlignment values.

Otherwise just use boost::any.

basic_any public construct/copy/destruct

  1. basic_any() noexcept;

    Postconditions:

    this->empty() is true.

  2. template<typename ValueType> basic_any(const ValueType & value);

    Makes a copy of value, so that the initial content of the new instance is equivalent in both type and value to value.

    Does not dynamically allocate if ValueType is nothrow move constructible and sizeof(value) <= OptimizeForSize and alignof(value) <= OptimizeForAlignment.

    Throws:

    std::bad_alloc or any exceptions arising from the copy constructor of the contained type.
  3. basic_any(const basic_any & other);

    Copy constructor that copies content of other into new instance, so that any content is equivalent in both type and value to the content of other, or empty if other is empty.

    Throws:

    May fail with a std::bad_alloc exception or any exceptions arising from the copy constructor of the contained type.
  4. basic_any(basic_any && other) noexcept;

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

    Requires:

    C++11 compatible compiler

    Postconditions:

    other->empty() is true

    Throws:

    Nothing.
  5. template<typename ValueType> 
      basic_any(ValueType && value, 
                typename boost::disable_if< boost::is_same< basic_any &, ValueType > >::type * = 0, 
                typename boost::disable_if< boost::is_const< ValueType > >::type * = 0);

    Forwards value, so that the initial content of the new instance is equivalent in both type and value to value before the forward.

    Does not dynamically allocate if ValueType is nothrow move constructible and sizeof(value) <= OptimizeForSize and alignof(value) <= OptimizeForAlignment.

    Requires:

    C++11 compatible compiler.

    Throws:

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

    Copies 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, or empty if rhs.empty().

    Throws:

    std::bad_alloc or any exceptions arising from the copy constructor of the contained type. Assignment satisfies the strong guarantee of exception safety.
  7. basic_any & operator=(basic_any && rhs) noexcept;

    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().

    Requires:

    C++11 compatible compiler.

    Postconditions:

    rhs->empty() is true

    Throws:

    Nothing.
  8. template<typename ValueType> basic_any & operator=(ValueType && rhs);

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

    Does not dynamically allocate if ValueType is nothrow move constructible and sizeof(value) <= OptimizeForSize and alignof(value) <= OptimizeForAlignment.

    Requires:

    C++11 compatible compiler.

    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. ~basic_any();

    Releases any and all resources used in management of instance.

    Throws:

    Nothing.

basic_any public member functions

  1. basic_any & swap(basic_any & rhs) noexcept;

    Exchange of the contents of *this and rhs.

    Returns:

    *this

    Throws:

    Nothing.
  2. bool empty() const noexcept;

    Returns:

    true if instance is empty, otherwise false.

    Throws:

    Nothing.
  3. void clear() noexcept;

    Postconditions:

    this->empty() is true

  4. 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