Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::any_cast

      From cppreference.com
      <cpp‎ |utility‎ |any
       
       
      Utilities library
       
       
      Defined in header<any>
      template<class T>
      T any_cast(const any& operand);
      (1)(since C++17)
      template<class T>
      T any_cast( any& operand);
      (2)(since C++17)
      template<class T>
      T any_cast( any&& operand);
      (3)(since C++17)
      template<class T>
      const T* any_cast(const any* operand)noexcept;
      (4)(since C++17)
      template<class T>
      T* any_cast( any* operand)noexcept;
      (5)(since C++17)

      Performs type-safe access to the contained object.

      LetU bestd::remove_cv_t<std::remove_reference_t<T>>.

      1) The program is ill-formed ifstd::is_constructible_v<T,const U&> isfalse.
      2) The program is ill-formed ifstd::is_constructible_v<T, U&> isfalse.
      3) The program is ill-formed ifstd::is_constructible_v<T, U> isfalse.
      4,5) The program is ill-formed ifstd::is_void_v<T> istrue.

      Contents

      [edit]Parameters

      operand - targetany object

      [edit]Return value

      1,2) Returnsstatic_cast<T>(*std::any_cast<U>(&operand)).
      3) Returnsstatic_cast<T>(std::move(*std::any_cast<U>(&operand))).
      4,5) Ifoperand is not a null pointer, and thetypeid of the requestedT matches that of the contents ofoperand, a pointer to the value contained by operand, otherwise a null pointer.

      [edit]Exceptions

      1-3) Throwsstd::bad_any_cast if thetypeid of the requestedT does not match that of the contents ofoperand.

      [edit]Example

      Run this code
      #include <any>#include <iostream>#include <string>#include <type_traits>#include <utility> int main(){// Simple exampleauto a1=std::any(12);std::cout<<"1) a1 is int: "<< std::any_cast<int>(a1)<<'\n'; try{auto s= std::any_cast<std::string>(a1);// throws}catch(conststd::bad_any_cast& e){std::cout<<"2) "<< e.what()<<'\n';} // Pointer exampleif(int* i= std::any_cast<int>(&a1))std::cout<<"3) a1 is int: "<<*i<<'\n';elseif(std::string* s= std::any_cast<std::string>(&a1))std::cout<<"3) a1 is std::string: "<<*s<<'\n';elsestd::cout<<"3) a1 is another type or unset\n"; // Advanced example    a1=std::string("hello");auto& ra= std::any_cast<std::string&>(a1);// reference    ra[1]='o'; std::cout<<"4) a1 is string: "<< std::any_cast<conststd::string&>(a1)<<'\n';// const reference auto s1= std::any_cast<std::string&&>(std::move(a1));// rvalue reference// Note: “s1” is a move-constructed std::string:    static_assert(std::is_same_v<decltype(s1),std::string>); // Note: the std::string in “a1” is left in valid but unspecified statestd::cout<<"5) a1.size(): "<< std::any_cast<std::string>(&a1)->size()// pointer<<'\n'<<"6) s1: "<< s1<<'\n';}

      Possible output:

      1) a1 is int: 122) bad any_cast3) a1 is int: 124) a1 is string: hollo5) a1.size(): 06) s1: hollo

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3305C++17the behavior of overloads(4,5) was unclear ifT isvoidthe program ill-formed in this case
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/any/any_cast&oldid=179373"

      [8]ページ先頭

      ©2009-2025 Movatter.jp