std::any::type(3)
π 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 atype_infoobject, 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms