So I've written a basic type tagging structure:
struct TypeTag
{
inline static size_t counter = 0;
template<typename T>
static size_t get()
{
static size_t value = counter++;
return value;
}
};
class c1 {};
class c2 {};
class c3 {};
class c4 {};
int main()
{
std::cout << "c1: " << TypeTag::get<c1>() << '\n';
std::cout << "c2: " << TypeTag::get<c2>() << '\n';
std::cout << "c3: " << TypeTag::get<c3>() << '\n';
std::cout << "c4: " << TypeTag::get<c4>() << '\n';
std::cout << "\n================================\n\n";
std::cout << "c1: " << TypeTag::get<c1>() << '\n';
std::cout << "c2: " << TypeTag::get<c2>() << '\n';
std::cout << "c3: " << TypeTag::get<c3>() << '\n';
std::cout << "c4: " << TypeTag::get<c4>() << '\n';
return 0;
}
Output:
c1: 0
c2: 1
c3: 2
c4: 3
================================
c1: 0
c2: 1
c3: 2
c4: 3
This will associate a type to a value.
Here's a working example of it: https://godbolt.org/z/9rqK6Pasz
Suppose we know the value at compile time: template <size_t value>
; is there any way to do the opposite? Is there any way to associate a variable value to a type?
template <size_t value>
struct TypeFrom
{
using type = ... // SOMETHING HERE
};