Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::any::any

      From cppreference.com
      <cpp‎ |utility‎ |any
       
       
      Utilities library
       
       
      constexpr any()noexcept;
      (1)(since C++17)
      any(const any& other);
      (2)(since C++17)
      any( any&& other)noexcept;
      (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 ValueType,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 newany object.

      1) Constructs an empty object.
      2,3) Copies(2) or moves(3) content ofother into a new instance, so that any content is equivalent in both type and value to those ofother prior to the constructor call, or empty ifother is empty. Formally,
      2) Ifother is empty, the constructed object is empty. Otherwise, equivalent toany(std::in_place_type<T>,std::any_cast<const T&>(other)), whereT is the type of the object contained inother.
      3) Ifother is empty, the constructed object is empty. Otherwise, the constructed object contains either the object contained inother, or an object of the same type constructed from the object contained inother, considering that object as an rvalue.
      4) Constructs an object with initial content an object of typestd::decay_t<ValueType>,direct-initialized fromstd::forward<ValueType>(value).
      5) Constructs an object with initial content an object of typestd::decay_t<ValueType>,direct-non-list-initialized fromstd::forward<Args>(args)....
      6) Constructs an object with initial content an object of typestd::decay_t<ValueType>,direct-non-list-initialized fromil,std::forward<Args>(args)....

      Contents

      [edit]Template parameters

      ValueType - contained value type
      Type requirements
      -
      std::decay_t<ValueType> must meet the requirements ofCopyConstructible.

      [edit]Parameters

      other - anotherany 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

      2,4-6) Throws any exception thrown by the constructor of the contained type.

      [edit]Notes

      Because the default constructor isconstexpr, staticstd::anys are initialized as part ofstatic non-local initialization, before any dynamic non-local initialization begins. This makes it safe to use an object of typestd::any in a constructor of any static object.

      [edit]Example

      Run this code
      #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// Required before C++20 for in-place construction    A(int age,std::string name,double salary): age(age), name(std::move(name)), salary(salary){}#endif}; // Using abi demangle to print nice type name of instance of any holdingvoid printType(conststd::any& a){std::cout<< boost::core::demangle(a.type().name())<<'\n';} int main(){// Constructor #4: std::any holding intstd::any a1{7}; // Constructor #5: std::any holding A, constructed in placestd::any a2(std::in_place_type<A>,30,"Ada",1000.25); // Constructor #6: std::any holding a set of A with custom comparisonauto 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);}

      Possible output:

      intAstd::set<A, main::{lambda(auto:1&&, auto:2&&)#1}, std::allocator<A> >

      [edit]See also

      assigns anany object
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/any/any&oldid=179367"

      [8]ページ先頭

      ©2009-2025 Movatter.jp