std::any::any - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::any::any

From cppreference.com
< cpp‎ | utility‎ | any

constexpr any(); | (1) | (since C++17)
any( const any& other ); | (2) | (since C++17)
any( any&& other ); | (3) | (since C++17)
template< class ValueType > any( ValueType&& value ); | (4) | (since C++17)
template< class ValueType, class... Args > explicit any( std::in_place_type_t<ValueType>, Args&&... args ); | (5) | (since C++17)
template< class T, class U, class... Args > explicit any( std::in_place_type_t<ValueType>, std::initializer_list<U> il, Args&&... args ); | (6) | (since C++17)

Constructs a new `any` object.

1) Constructs an empty object.
2-3) Copies (2) or moves (3) content of `other` into a new instance, so that any content is equivalent in both type and value to those of `other` prior to the constructor call, or empty if `other` is empty. Formally,
2) If `other` is empty, the constructed object is empty. Otherwise, equivalent to any(std::in_place<T>, std::any_cast<const T&>(other)), where `T` is the type of the object contained in `other`.
3) If `other` is empty, the constructed object is empty. Otherwise, the constructed object contains either the object contained in `other`, or an object of the same type constructed from the object contained in `other`, considering that object as an rvalue.
4) Constructs an object with initial content an object of type std::decay_t<ValueType>, direct-initialized from std::forward<ValueType>(value). If std::is_copy_constructible<std::decay_t<ValueType>>::value is false, the program is ill-formed. This overload only participates in overload resolution if std::decay_t<ValueType> is not the same type as any nor a specialization of std::in_place_type_t, and std::is_copy_constructible_v<std::decay_t<ValueType>> is true.
5) Constructs an object with initial content an object of type `std::decay_t<ValueType>`, direct-non-list-initialized from std::forward<Args>(args).... This overload only participates in overload resolution if std::is_constructible_v<std::decay_t<ValueType>, Args...> and std::is_copy_constructible_v<std::decay_t<ValueType>> are both true.
6) Constructs an object with initial content an object of type `std::decay_t<ValueType>`, direct-non-list-initialized from il, std::forward<Args>(args).... This overload only participates in overload resolution if std::is_constructible_v<std::decay_t<ValueType>, std::initializer_list<U>&, Args...> and std::is_copy_constructible_v<std::decay_t<ValueType>> are both true.

### [edit] Template parameters

ValueType | - | contained value type
Type requirements
- `std::decay_t<ValueType>` must meet the requirements of `CopyConstructible`.

### [edit] Parameters

other | - | another `any` object to copy or move from
value | - | value to initialize the contained value with
il, args | - | arguments to be passed to the constructor of the contained object

### [edit] Exceptions

1,3)
`noexcept` specification:
noexcept
2,4,5,6) Throws any exception thrown by the constructor of the contained type.

### [edit] Notes

Because the default constructor is `constexpr`, static `std::any`s are initialized as part of static non-local initialization, before any dynamic non-local initialization begins. This makes it safe to use an object of type `std::any` in a constructor of any static object.

### [edit] See also

operator= | assigns an `any` object (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/utility/any/any&oldid=91720"
Category:

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template