添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
重感情的板凳  ·  scala ...·  2 天前    · 
咆哮的牛肉面  ·  SignalR Hub 在 .NET ...·  22 小时前    · 
大方的铁板烧  ·  Job Artifacts API | ...·  18 小时前    · 
强健的碗  ·  Unity - Scripting ...·  2 月前    · 
老实的显示器  ·  OpenLayers 6 ...·  6 月前    · 
聪明的花生  ·  AGI Explained·  6 月前    · 
挂过科的酱肘子  ·  GPT2代码详解 - ...·  1 年前    · 
Filesystem library (C++17) Regular expressions library (C++11) Concurrency support library (C++11) Technical specifications Symbols index External libraries
Utilities library

Performs type-safe access to the contained object.

Let U be std:: remove_cv_t < std:: remove_reference_t < T >> .

1) The program is ill-formed if std:: is_constructible_v < T, const U & > is false .
2) The program is ill-formed if std:: is_constructible_v < T, U & > is false .
3) The program is ill-formed if std:: is_constructible_v < T, U > is false .
4,5) The program is ill-formed if std:: is_void_v < T > is true .
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20) (C++20) (C++20)
(C++20) (C++20) (C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20) (C++20) (C++20)
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

Contents

  • 1 Parameters
  • 2 Return value
  • 3 Exceptions
  • 4 Example
  • 5 Defect reports
  • [ edit ] Parameters

    [ edit ] Return value

    1,2) Returns static_cast < T > ( * std :: any_cast < U > ( & operand ) ) .
    3) Returns static_cast < T > ( std :: move ( * std :: any_cast < U > ( & operand ) ) ) .
    4,5) If operand is not a null pointer, and the typeid of the requested T matches that of the contents of operand , a pointer to the value contained by operand, otherwise a null pointer.

    [ edit ] Exceptions

    1-3) Throws std::bad_any_cast if the typeid of the requested T does not match that of the contents of operand .

    [ edit ] Example

    #include <any>
    #include <iostream>
    #include <string>
    #include <type_traits>
    #include <utility>
    int main()
        // Simple example
        auto a1 = std::any(12);
        std::cout << "1) a1 is int: " << std::any_cast<int>(a1) << '\n';
            auto s = std::any_cast<std::string>(a1); // throws
        catch (const std::bad_any_cast& e)
            std::cout << "2) " << e.what() << '\n';
        // Pointer example
        if (int* i = std::any_cast<int>(&a1))
            std::cout << "3) a1 is int: " << *i << '\n';
        else if (std::string* s = std::any_cast<std::string>(&a1))
            std::cout << "3) a1 is std::string: " << *s << '\n';
            std::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<std::string const&>(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 state
        std::cout << "5) a1.size(): "
                  << std::any_cast<std::string>(&a1)->size() //< pointer
                  << '\n'
                  << "6) s1: " << s1 << '\n';
    

    Possible output:

    1) a1 is int: 12
    2) bad any_cast
    3) a1 is int: 12
    4) a1 is string: hollo
    5) a1.size(): 0
    6) s1: hollo

    [edit] Defect reports

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

    Applied to Behavior as published Correct behavior LWG 3305 C++17 the behavior of overloads ( 4,5 ) was unclear if T is void the program ill-formed in this case
    Toolbox
  • What links here
  • Related changes
  • Upload file
  • Special pages
  • Printable version
  • Page information
    • In other languages
  • Español
  • 日本語
  • Русский
  • 中文
  •