std::experi...al::any_cast(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

okay so bestie, welcome to the C++ Standard Libary manual (yes, that spelling is in the header, not me 😭) β€” this is the std::experi...al::any_cast(3) page, and we're talking about std::experimental::any_cast. Buckle up, it's giving type-safe access realness ✨

πŸŽ€ NAME πŸŽ€

std::experimental::any_cast β€” literally std::experimental::any_cast, the function this whole page is about. No cap.

✨ Synopsis ✨

Here's the whole overload lineup, bestie β€” all five are from the library fundamentals TS:

          template<class ValueType>                              (1) (library
       fundamentals TS)
              ValueType any_cast(const any& operand);
          template<class ValueType>                              (2) (library
       fundamentals TS)
              ValueType any_cast(any& operand);
          template<class ValueType>                              (3) (library
       fundamentals TS)
              ValueType any_cast(any&& operand);
          template<class ValueType>
              const ValueType* any_cast(const any* operand)      (4) (library
       fundamentals TS)
          noexcept;
          template<class ValueType>                              (5) (library
       fundamentals TS)
              ValueType* any_cast(any* operand) noexcept;

Performs type-safe access to the contained object. That's the whole vibe.

And for overloads (1-3), the program is ill-formed if ValueType is not a reference and std::is_copy_constructible<ValueType>::value is false. Like, do not pass go, do not collect $200, the compiler is gonna side-eye you πŸ’€

🌸 Parameters 🌸

  • operand β€” target any object. That's it, that's the parameter, she's simple and iconic.

πŸ’– Return value πŸ’–

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

😭 Exceptions 😭

  • 1-3) Throws bad_any_cast if the typeid of the requested ValueType does not match that of the contents of operand.

🏷️ Category 🏷️

  • Noindexed pages

source/version + date tea: http://cppreference.com β€” 2024.06.10. Bye bestie, go slay your any casts πŸ’…πŸ«Ά

πŸ“¦ where this page comes from