| Home | Libraries | People | FAQ | More

## Class template any

boost::type_erasure::any

## Synopsis

    // In header: <boost/type_erasure/call.hpp>

    template<typename Concept, typename Placeholder>
    class any {
    public:
      // construct/copy/destruct
      any();
      template<typename U> any(U &&);
      template<typename U, typename Map> any(U &&, const static_binding< Map > &);
      any(const any &);
      template<typename Concept2, typename Tag2>
        any(const any< Concept2, Tag2 > &);
      template<typename Concept2, typename Tag2, typename Map>
        any(const any< Concept2, Tag2 > &, const static_binding< Map > &);
      template<typename Concept2, typename Tag2>
        any(const any< Concept2, Tag2 > &, const binding< Concept > &);
      template<class... U> explicit any(U &&...);
      template<class... U> explicit any(const binding< Concept > &, U &&...);
      any & operator=(const any &);
      template<typename U> any & operator=(const U &);
      ~any();
    };

## Description

The class template any can store any object that models a specific Concept. It dispatches all the functions defined by the Concept to the contained type at runtime.

See Also:

concept_of, placeholder_of, any_cast, is_empty, binding_of, typeid_of

### Template Parameters

1.     typename Concept

   The Concept that the stored type should model.

2.     typename Placeholder

### `any` public construct/copy/destruct

1.     any();

   Constructs an empty `any`.

   Except as otherwise noted, all operations on an empty `any` result in a `bad_function_call` exception. The copy-constructor of an empty `any` creates another null `any`. The destructor of an empty `any` is a no-op. Comparison operators treat all empty `anys` as equal. `typeid_of` applied to an empty `any` returns `typeid(void)`.

   An `any` which does not include `relaxed` in its Concept can never be null.

   See Also:

   `is_empty`

   Requires: | relaxed must be in `Concept`.
   Throws: | Nothing.

2.     template<typename U> any(U && data);

   Constructs an `any` to hold a copy of `data`. The `Concept` will be instantiated with the placeholder `T` bound to U.

   | Note
   This constructor never matches if the argument is an `any`, `binding`, or `static_binding`.

   Parameters:

   `data` | The object to store in the `any`.

   Requires: | `U` is a model of `Concept`. `U` must be CopyConstructible. `Concept` must not refer to any non-deduced placeholder besides `T`.
   Throws: | std::bad_alloc or whatever that the copy constructor of `U` throws.

3.     template<typename U, typename Map>
         any(U && data, const static_binding< Map > & binding);

   Constructs an `any` to hold a copy of `data` with explicitly specified placeholder bindings.

   | Note
   This constructor never matches if the argument is an `any`.

   Parameters:

   `binding` | Specifies the types that all the placeholders should bind to.
   `data` | The object to store in the `any`.

   Requires: | `U` is a model of `Concept`. `U` must be CopyConstructible. `Map` is an MPL map with an entry for every non-deduced placeholder referred to by `Concept`. ``T` must` map to `U` in `Map`.
   Throws: | std::bad_alloc or whatever that the copy constructor of `U` throws.

4.     any(const any & other);

   Copies an `any`.

   Parameters:

   `other` | The object to make a copy of.

   Requires: | `Concept` must contain constructible<T(const T&)>. (This is included in copy_constructible<T>)
   Throws: | std::bad_alloc or whatever that the copy constructor of the contained type throws.

5.     template<typename Concept2, typename Tag2>
         any(const any< Concept2, Tag2 > & other);

   Upcasts from an `any` with stricter requirements to an `any` with weaker requirements.

   Parameters:

   `other` | The object to make a copy of.

   Requires: | `Concept` must contain constructible<T(const T&)>. `Concept` must not refer to any non-deduced placeholder besides `T`. After substituting `T` for `Tag2`, the requirements of `Concept2` must be a superset of the requirements of `Concept`.
   Throws: | std::bad_alloc or whatever that the copy constructor of the contained type throws.

6.     template<typename Concept2, typename Tag2, typename Map>
         any(const any< Concept2, Tag2 > & other,
             const static_binding< Map > & binding);

   Constructs an `any` from another `any`.

   Parameters:

   `binding` | Specifies the mapping between the placeholders used by the two concepts.
   `other` | The object to make a copy of.

   Requires: | `Concept` must contain constructible<T(const T&)>. `Map` must be an MPL map with keys for all the non-deduced placeholders used by `Concept` and values for the corresponding placeholders in `Concept2`. After substituting placeholders according to `Map`, the requirements of `Concept2` must be a superset of the requirements of `Concept`.
   Throws: | std::bad_alloc or whatever that the copy constructor of the contained type throws.

7.     template<typename Concept2, typename Tag2>
         any(const any< Concept2, Tag2 > & other, const binding< Concept > & binding);

   Constructs an `any` from another `any`.

   | Warning
   This constructor is potentially dangerous, as it cannot check at compile time whether the arguments match.

   Parameters:

   `binding` | Specifies the bindings of placeholders to actual types.
   `other` | The object to make a copy of.

   Requires: | `Concept` must contain constructible<T(const T&)>. The type stored in `other` must match the type expected by `binding`.
   Postconditions: | binding_of(*this) == `binding`
   Throws: | std::bad_alloc or whatever that the copy constructor of the contained type throws.

8.     template<class... U> explicit any(U &&... arg);

   Calls a constructor of the contained type. The bindings will be deduced from the arguments.

   | Note
   This constructor is never chosen if any other constructor can be called instead.

   Parameters:

   `arg` | The arguments to be passed to the underlying constructor.

   Requires: | `Concept` must contain an instance of constructible which can be called with these arguments. At least one of the arguments must by an any with the same `Concept` as this. The bindings of all the arguments that are any's, must be the same.
   Throws: | std::bad_alloc or whatever that the constructor of the contained type throws.

9.     template<class... U>
         explicit any(const binding< Concept > & binding, U &&... arg);

   Calls a constructor of the contained type.

   Parameters:

   `arg` | The arguments to be passed to the underlying constructor.
   `binding` | Specifies the bindings of placeholders to actual types.

   Requires: | `Concept` must contain a matching instance of constructible. The contained type of every argument that is an any, must be the same as that specified by `binding`.
   Postconditions: | binding_of(*this) == `binding`
   Throws: | std::bad_alloc or whatever that the constructor of the contained type throws.

10.     any & operator=(const any & other);

    Assigns to an `any`.

    If an appropriate overload of `assignable` is not available and `relaxed` is in `Concept`, falls back on constructing from `other`.

    Throws: | Whatever the assignment operator of the contained type throws. When falling back on construction, throws `std::bad_alloc` or whatever the copy constructor of the contained type throws. In this case assignment provides the strong exception guarantee. When calling the assignment operator of the contained type, the exception guarantee is whatever the contained type provides.

11.     template<typename U> any & operator=(const U & other);

    Assigns to an `any`.

    If an appropriate overload of `assignable` is not available and `relaxed` is in `Concept`, falls back on constructing from `other`.

    Throws: | Whatever the assignment operator of the contained type throws. When falling back on construction, throws `std::bad_alloc` or whatever the copy constructor of the contained type throws. In this case assignment provides the strong exception guarantee. When calling an assignment operator of the contained type, the exception guarantee is whatever the contained type provides.

12.     ~any();

    Requires: | `Concept` includes destructible<T>.