Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::any

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
       
      Defined in header<any>
      class any;
      (since C++17)

      The classany describes a type-safe container for single values of anycopy constructible type.

      1) An object of classany stores an instance of any type that satisfies the constructor requirements or is empty, and this is referred to as thestate of the classany object. The stored instance is called the contained object. Two states are equivalent if they are either both empty or if both are not empty and if the contained objects are equivalent.
      2) The non-memberany_cast functions provide type-safe access to the contained object.

      Typically, implementations apply small objects optimization (avoid dynamic allocations) to types for whichstd::is_nothrow_move_constructible istrue.

      Contents

      [edit]Member functions

      constructs anany object
      (public member function)[edit]
      assigns anany object
      (public member function)[edit]
      destroys anany object
      (public member function)[edit]
      Modifiers
      change the contained object, constructing the new object directly
      (public member function)[edit]
      destroys contained object
      (public member function)[edit]
      swaps twoany objects
      (public member function)[edit]
      Observers
      checks if object holds a value
      (public member function)[edit]
      returns thetypeid of the contained value
      (public member function)[edit]

      [edit]Non-member functions

      specializes thestd::swap algorithm
      (function)[edit]
      (C++17)
      type-safe access to the contained object
      (function template)[edit]
      (C++17)
      creates anany object
      (function template)[edit]

      [edit]Helper classes

      exception thrown by the value-returning forms ofany_cast on a type mismatch
      (class)[edit]

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_any201606L(C++17)std::any

      [edit]Example

      Run this code
      #include <any>#include <iostream> int main(){std::cout<<std::boolalpha; // any type    std::any a=1;std::cout<< a.type().name()<<": "<<std::any_cast<int>(a)<<'\n';    a=3.14;std::cout<< a.type().name()<<": "<<std::any_cast<double>(a)<<'\n';    a=true;std::cout<< a.type().name()<<": "<<std::any_cast<bool>(a)<<'\n'; // bad casttry{        a=1;std::cout<<std::any_cast<float>(a)<<'\n';}catch(conststd::bad_any_cast& e){std::cout<< e.what()<<'\n';} // has value    a=2;if(a.has_value())std::cout<< a.type().name()<<": "<<std::any_cast<int>(a)<<'\n'; // reset    a.reset();if(!a.has_value())std::cout<<"no value\n"; // pointer to contained data    a=3;int* i=std::any_cast<int>(&a);std::cout<<*i<<'\n';}

      Possible output:

      int: 1double: 3.14bool: truebad any_castint: 2no value3

      [edit]See also

      (C++11)
      copyable wrapper of any copy constructible callable object
      (class template)[edit]
      move-only wrapper of any callable object that supports qualifiers in a given call signature
      (class template)[edit]
      (C++17)
      a type-safe discriminated union
      (class template)[edit]
      (C++17)
      a wrapper that may or may not hold an object
      (class template)[edit]
      (C++11)
      smart pointer with unique object ownership semantics
      (class template)[edit]
      (C++26)
      a wrapper containing dynamically-allocated object with value-like semantics
      (class template)[edit]
      a polymorphic wrapper containing dynamically-allocated object with value-like semantics
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/any&oldid=182981"

      [8]ページ先頭

      ©2009-2025 Movatter.jp