std::any::type(3)

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

πŸ’… NAME πŸ’…

okay so bestie, this is the std::any::type(3) page from the C++ Standard Libary manual β€” yes, we're folding the manual name in right here like a little name tag. The NAME is std::any::type, and the thing it names is also std::any::type. Self-titled icon behavior, honestly. πŸ’–


✨ Synopsis ✨

const std::type_info& type() const noexcept;  (since C++17)

It queries the contained type. That's the whole vibe.


πŸŽ€ Parameters πŸŽ€

none, bestie β€” literally (none). No arguments, no stress. 🫢


πŸ’– Return value πŸ’–

It gives you the typeid of the contained value if the instance is non-empty. Otherwise? You get typeid(void). No cap. ✨


πŸ¦‹ Example πŸ¦‹

This example demonstrates the std::any visitor idiom β€” with the ability to register new visitors at compile- and run-time. Obsessed, honestly. 🌸

Run this code:

       // Run this code

        #include <any>
        #include <functional>
        #include <iomanip>
        #include <iostream>
        #include <type_traits>
        #include <typeindex>
        #include <typeinfo>
        #include <unordered_map>
        #include <vector>

        template<class T, class F>
        inline std::pair<const std::type_index, std::function<void(std::any
       const&)>>
            to_any_visitor(F const& f)
        {
            return
            {
                std::type_index(typeid(T)),
                [g = f](std::any const& a)
                {
                    if constexpr (std::is_void_v<T>)
                        g();
                    else
                        g(std::any_cast<T const&>(a));
                }
            };
        }

        static std::unordered_map<std::type_index, std::function<void(std::any
       const&)>>
            any_visitor
        {
            to_any_visitor<void>([] { std::cout << "{}"; }),
            to_any_visitor<int>([](int x) { std::cout << x; }),
            to_any_visitor<unsigned>([](unsigned x) { std::cout << x; }),
            to_any_visitor<float>([](float x) { std::cout << x; }),
            to_any_visitor<double>([](double x) { std::cout << x; }),
            to_any_visitor<char const*>([](char const* s)
                { std::cout << std::quoted(s); }),
            // ... add more handlers for your types ...
        };

        inline void process(const std::any& a)
        {
            if (const auto it = any_visitor.find(std::type_index(a.type()));
                it != any_visitor.cend())
                it->second(a);
            else
                std::cout << "Unregistered type "<<
       std::quoted(a.type().name());
        }

        template<class T, class F>
        inline void register_any_visitor(F const& f)
        {
            std::cout << "Register visitor for type "
                      << std::quoted(typeid(T).name()) << '\n';
            any_visitor.insert(to_any_visitor<T>(f));
        }

        int main()
        {
            std::vector<std::any> va{{}, 42, 123u, 3.14159f, 2.71828, "C++17"};

            std::cout << "{ ";
            for (const std::any& a : va)
            {
                process(a);
                std::cout << ", ";
            }
            std::cout << "}\n";

            process(std::any(0xFULL)); //< Unregistered type "y" (unsigned long
       long long)
            std::cout << '\n';

            register_any_visitor<unsigned long long>([](auto x)
            {
                std::cout << std::hex << std::showbase << x;
            });

            process(std::any(0xFULL)); //< OK: 0xf
            std::cout << '\n';
        }

🌈 Possible output 🌈

{ {}, 42, 123, 3.14159, 2.71828, "C++17", }
Unregistered type "y"
Register visitor for type "y"
0xf

πŸ‘‘ See also πŸ‘‘

  • type_index β€” a wrapper around a type_info object, that can be used as an index in (C++11) associative and unordered associative containers (class). πŸ‘‘

source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::any::type(3) β€” love u, bye bestie πŸ’‹πŸ’–

πŸ“¦ where this page comes from