std::any::any
ja.cppreference.net より
constexpr any() noexcept;
|
(1) | (C++17以上) |
any( const any& other );
|
(2) | (C++17以上) |
any( any&& other ) noexcept;
|
(3) | (C++17以上) |
template< class ValueType >
any( ValueType&& value );
|
(4) | (C++17以上) |
template< class ValueType, class... Args >
explicit any( std::in_place_type_t<ValueType>, Args&&... args );
|
(5) | (C++17以上) |
template< class ValueType, class U, class... Args >
explicit any( std::in_place_type_t<ValueType>, std::initializer_list<U> il,
Args&&... args );
|
(6) | (C++17以上) |
新しい any オブジェクトを構築します。
1) 空のオブジェクトを構築します。
2,3) (2) または (3)
other の内容を新しいインスタンスにコピーまたはムーブし、コンストラクタ呼び出し前の other と型および値の両方で等価な内容にするか、other が空の場合は空にします。形式的には、2)
other が空の場合、構築されるオブジェクトは空です。そうでなければ、any(std::in_place_type<T>, std::any_cast<const T&>(other)) と等価です。ここで T は other に含まれるオブジェクトの型です。3)
other が空の場合、構築されるオブジェクトは空です。そうでなければ、構築されるオブジェクトは other に含まれるオブジェクトか、other に含まれるオブジェクトからそのオブジェクトを右辺値として扱って構築された同じ型のオブジェクトを含みます。4) 初期内容として型
std::decay_t<ValueType> のオブジェクトを 直接初期化 で std::forward<ValueType>(value) から構築します。
- このオーバーロードは、
std::decay_t<ValueType>がanyと同じ型でも std::in_place_type_t の特殊化でもなく、かつstd::is_copy_constructible_v<std::decay_t<ValueType>>がtrueである場合にのみオーバーロード解決に参加します。
5) 初期内容として型
std::decay_t<ValueType> のオブジェクトを 直接非リスト初期化 で std::forward<Args>(args)... から構築します。
- このオーバーロードは、
std::is_constructible_v<std::decay_t<ValueType>, Args...>とstd::is_copy_constructible_v<std::decay_t<ValueType>>の両方がtrueである場合にのみオーバーロード解決に参加します。
6) 初期内容として型
std::decay_t<ValueType> のオブジェクトを 直接非リスト初期化 で il, std::forward<Args>(args)... から構築します。
- このオーバーロードは、
std::is_constructible_v<std::decay_t<ValueType>, std::initializer_list<U>&, Args...>とstd::is_copy_constructible_v<std::decay_t<ValueType>>の両方がtrueである場合にのみオーバーロード解決に参加します。
テンプレートパラメータ
| ValueType | - | 含まれる値の型 |
| 型要件 | ||
-
std::decay_t<ValueType>
must meet the requirements of
CopyConstructible
.
|
||
パラメータ
| other | - |
コピーまたは移動元の別の
any
オブジェクト
|
| value | - | 格納される値を初期化するための値 |
| il, args | - | 格納オブジェクトのコンストラクタに渡される引数 |
例外
2,4-6)
格納されている型のコンストラクタによってスローされるあらゆる例外をスローします。
注記
デフォルトコンストラクタは
constexpr
であるため、静的
std::any
は
静的非ローカル初期化
の一部として、動的非ローカル初期化が開始される前に初期化されます。これにより、任意の静的オブジェクトのコンストラクタ内で
std::any
型のオブジェクトを安全に使用できます。
例
このコードを実行
#include <boost/core/demangle.hpp> #include <any> #include <initializer_list> #include <iostream> #include <memory> #include <set> #include <string> #include <utility> struct A { int age; std::string name; double salary; #if __cpp_aggregate_paren_init < 201902L // C++20以前ではインプレース構築に必要 A(int age, std::string name, double salary) : age(age), name(std::move(name)), salary(salary) {} #endif }; // abi demangleを使用して、anyが保持するインスタンスの型名を整形して表示 void printType(const std::any& a) { std::cout << boost::core::demangle(a.type().name()) << '\n'; } int main() { // コンストラクタ #4: intを保持するstd::any std::any a1{7}; // コンストラクタ #5: インプレース構築されたAを保持するstd::any std::any a2(std::in_place_type<A>, 30, "Ada", 1000.25); // コンストラクタ #6: カスタム比較関数を持つAのsetを保持するstd::any auto lambda = [](auto&& l, auto&& r){ return l.age < r.age; }; std::any a3( std::in_place_type<std::set<A, decltype(lambda)>>, { A{39, std::string{"Ada"}, 100.25}, A{20, std::string{"Bob"}, 75.5} }, lambda); printType(a1); printType(a2); printType(a3); }
出力例:
int
A
std::set<A, main::{lambda(auto:1&&, auto:2&&)#1}, std::allocator<A> >
関連項目
anyオブジェクトを代入
(公開メンバ関数) |