Search code examples
c++templatesstructtemplate-meta-programming

Is There any Way to associate a Value to a Type?


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 
};

Solution

  • As shown, this is not possible for some fairly fundamental reasons: C++ simply does not work this way.

    The value of TypeTag::get<T> is known only at runtime. The value for a given T depends on the order of the first call to a template instance for this particular T relative to first calls for other types. It's entirely possible that each time the program runs the same types get different TypeTags. This is fundamental to C++: a statically-scoped object is constructed the first time its scope gets entered. The fact that the scope is generated from a template is immaterial. Effectively counter++ gets evaluated, to initialize the static size_t value the first time its get() gets called.

    So, when all is said and done, a particular TypeTag::get<T> value is known only at runtime.

    But templates parameters (such as the size_t n in ValueType<n>) must be known at compile time. This is fundamental to C++. The End.