Home:ALL Converter>When is a type equal (template specialization)?

When is a type equal (template specialization)?

Ask Time:2015-05-31T22:09:56         Author:Michael

Json Formatter

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?

Author:Michael,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30558373/when-is-a-type-equal-template-specialization
m.s. :

CONFIG1 and CONFIG3 are the same type, therefore your specialization will fail with\n\nerror: redefinition of 'struct MyClass<Config<x, y, z> >'\nstruct MyClass<CONFIG3>{};\n\n\nYou can use inheritance to create a new type:\n\nusing CONFIG1 = Config<x, y, z>;\nstruct CONFIG3 : CONFIG1{}; \n\n\nLive example: https://ideone.com/4GrlaW",
2015-05-31T14:19:41
Xiaotian Pei :

CONFIG1 and CONFIG3 are the same type. If you doubt, you can verify with std::is_same",
2015-05-31T14:18:01
yy