Metaprogramming header-only library to obtain constexpr
textual string representations of enum types and type names, including smart pointers and complex STL compound types.
Back to list of all libraries | See all modules
Library `mrpt-typemeta`
[New in MRPT 2.0.0]
This library is part of MRPT but has no dependencies, so it can be installed in Debian-based systems with:
sudo apt install libmrpt-typemeta-dev
See: Using MRPT from your CMake project
Example #1: compile-time type/struct/class names to strings
Use mrpt::typemeta::TTypeName to extract a constexpr
string with a compiler-independent representation of arbitrarily-complex types and STL containers. Note that creating objects from a run-time string representation of its type is handled in a different library ([mrpt-serialization]).
See: typemeta_TTypeName/test.cpp
#include <iostream>
#include <memory>
{
};
{
struct MyBarClass
{
};
struct MyBarClass2
{
};
}
{
cout << s1 << endl;
cout << TTypeName<set<vector<double>>>::get() << endl;
cout << TTypeName<MyFooClass>::get() << endl;
cout << TTypeName<MyFooClass::Ptr>::get() << endl;
cout << TTypeName<MyNS::MyBarClass>::get() << endl;
cout << TTypeName<MyNS::MyBarClass2>::get() << endl;
cout << TTypeName<double>::get() << endl;
cout << TTypeName<vector<double>>::get() << endl;
cout << TTypeName<array<int32_t, 5>>::get() << endl;
cout << TTypeName<set<double>>::get() << endl;
cout << TTypeName<pair<int32_t, pair<int32_t, int32_t>>>::get() << endl;
cout << TTypeName<map<double, set<int32_t>>>::get() << endl;
multimap<double, pair<MyFooClass, MyNS::MyBarClass2>>>>::get()
<< endl;
}
Output:
int32_t
std::set<std::vector<double>>
MyFooClass
std::shared_ptr<MyFooClass>
MyNS::MyBarClass
MyNS::MyBarClass2
double
std::vector<double>
std::array<int32_t,5>
std::set<double>
std::pair<int32_t,std::pair<int32_t,int32_t>>
std::map<double,std::set<int32_t>>
std::set<std::multimap<double,std::pair<MyFooClass,MyNS::MyBarClass2>>>
Example #2: compile-time constexpr strings manipulation
See: typemeta_StaticString/test.cpp
#include <iostream>
{
cout << "string_literal<3>=" << s << endl;
auto ab = a + b;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "a+b=" << ab << endl;
static_assert(ab.size() == 6, "***");
cout << "(a+b).size()=" << ab.size() << endl;
cout << "(a+b)[0]=" << ab[0] << endl;
cout << "(a+b)[5]=" << ab[5] << endl;
}
Output:
string_literal<3>=foo
a=foo
b=bar
a+b=foobar
(a+b).size()=6
(a+b)[0]=f
(a+b)[5]=r
Example #3: compile-time numbers to strings
See: typemeta_StaticString/test.cpp
#include <iostream>
{
cout << "42 as string=" << n42 << endl;
cout << "9999 as string=" << n9999 << endl;
}
Output:
42 as string=42
9999 as string=9999
Example #4: enum values to/from strings
See: typemeta_TEnumType/test.cpp
#include <iostream>
#include <string>
{
};
{
};
{
<< endl;
<< endl;
<< endl;
<< endl;
}
Output:
White => 15
Black => 0
Gray => 7
7 <= Gray