std::any:: any
|
constexpr
any
(
)
noexcept
;
|
(1) | (seit C++17) |
|
any
(
const
any
&
other
)
;
|
(2) | (seit C++17) |
|
any
(
any
&&
other
)
noexcept
;
|
(3) | (seit C++17) |
|
template
<
class
ValueType
>
any ( ValueType && value ) ; |
(4) | (seit C++17) |
|
template
<
class
ValueType,
class
...
Args
>
explicit any ( std:: in_place_type_t < ValueType > , Args && ... args ) ; |
(5) | (seit C++17) |
|
template
<
class
ValueType,
class
U,
class
...
Args
>
explicit
any
(
std::
in_place_type_t
<
ValueType
>
,
std::
initializer_list
<
U
>
il,
|
(6) | (seit C++17) |
Konstruiert ein neues
any
-Objekt.
T
der Typ des in
other
enthaltenen Objekts ist.
-
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn
std::
decay_t
<
ValueType
>
nicht denselben Typ wie
anyhat noch eine Spezialisierung von std::in_place_type_t ist, und std:: is_copy_constructible_v < std:: decay_t < ValueType >> true ist.
- Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn std:: is_constructible_v < std:: decay_t < ValueType > , Args... > und std:: is_copy_constructible_v < std:: decay_t < ValueType >> beide true sind.
- Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn std:: is_constructible_v < std:: decay_t < ValueType > , std:: initializer_list < U > & , Args... > und std:: is_copy_constructible_v < std:: decay_t < ValueType >> beide true sind.
Inhaltsverzeichnis |
Template-Parameter
| ValueType | - | enthaltene Wertart |
| Typanforderungen | ||
-
std::decay_t<ValueType>
muss die Anforderungen von
CopyConstructible
erfüllen.
|
||
Parameter
| other | - |
ein anderes
any
Objekt, von dem kopiert oder verschoben wird
|
| value | - | Wert zur Initialisierung des enthaltenen Werts |
| il, args | - | Argumente, die an den Konstruktor des enthaltenen Objekts übergeben werden |
Ausnahmen
Hinweise
Da der Standardkonstruktor
constexpr
ist, werden statische
std::any
s als Teil der
statischen Nicht-Lokal-Initialisierung
initialisiert, bevor jegliche dynamische Nicht-Lokal-Initialisierung beginnt. Dies macht es sicher, ein Objekt vom Typ
std::any
im Konstruktor eines beliebigen statischen Objekts zu verwenden.
Beispiel
#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 // Erforderlich vor C++20 für In-Place-Konstruktion A(int age, std::string name, double salary) : age(age), name(std::move(name)), salary(salary) {} #endif }; // Verwendung von abi demangle zur Ausgabe lesbarer Typnamen von any-Instanzen void printType(const std::any& a) { std::cout << boost::core::demangle(a.type().name()) << '\n'; } int main() { // Konstruktor #4: std::any enthält int std::any a1{7}; // Konstruktor #5: std::any enthält A, direkt konstruiert std::any a2(std::in_place_type<A>, 30, "Ada", 1000.25); // Konstruktor #6: std::any enthält eine Menge von A mit benutzerdefiniertem Vergleich 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); }
Mögliche Ausgabe:
int
A
std::set<A, main::{lambda(auto:1&&, auto:2&&)#1}, std::allocator<A> >
Siehe auch
weist ein
any
Objekt zu
(öffentliche Elementfunktion) |