Search code examples
c++templatestypedefusing

When is a type equal (template specialization)?


I store a configuration as a type:

using CONFIG1 = Config<x, y, z>;
using CONFIG2 = Config<a, b, c>;
using CONFIG3 = Config<x, y, z>;

For each config, there's a class template specialization, which does some more configuration work:

template <class CONFIG>
MyClass;

template <>
MyClass<CONFIG1>{...}

template <>
MyClass<CONFIG2>{...}

template <>
MyClass<CONFIG3>{...}

Now, as you see, CONFIG1 happens to have the same definition as CONFIG3.

The question is:

  1. Which specialization will be taken for CONFIG1 and CONFIG3, or: When is a type equal? Is it it's name or it's actual content?

  2. If it's the actual content, how can I achieve that CONFIG1 and CONFIG3 actually invoke different specializations?


Solution

  • CONFIG1 and CONFIG3 are the same type, therefore your specialization will fail with

    error: redefinition of 'struct MyClass<Config<x, y, z> >'
    struct MyClass<CONFIG3>{};
    

    You can use inheritance to create a new type:

    using CONFIG1 = Config<x, y, z>;
    struct CONFIG3 : CONFIG1{}; 
    

    Live example: https://ideone.com/4GrlaW