std::any:: any
|
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,
|
(6) | (C++17以降) |
新しい
any
オブジェクトを構築します。
T
は
other
に含まれるオブジェクトの型である。
-
このオーバーロードは、
std::
decay_t
<
ValueType
>
が
anyと同じ型ではなく、かつ std::in_place_type_t の特殊化でもなく、さらに std:: is_copy_constructible_v < std:: decay_t < ValueType >> が true である場合にのみ、オーバーロード解決に参加します。
- このオーバーロードは、 std:: is_constructible_v < std:: decay_t < ValueType > , Args... > と std:: is_copy_constructible_v < std:: decay_t < ValueType >> の両方が true である場合にのみ、オーバーロード解決に参加する。
- このオーバーロードは、 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 | - | 格納オブジェクトのコンストラクタに渡される引数 |
例外
注記
デフォルトコンストラクタは
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
オブジェクトを代入
(公開メンバ関数) |