什么时候类型相等(模板特化)?

When is a type equal (template specialization)?

我将配置存储为类型:

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

对于每个配置,都有一个 class 模板专业化,它会做一些更多的配置工作:

template <class CONFIG>
MyClass;

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

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

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

现在,如您所见,CONFIG1 恰好与 CONFIG3 具有相同的定义。

问题是:

  1. CONFIG1CONFIG3将采用哪个专业化,或者:什么时候类型相等?是它的名字吗还是实际内容?

  2. 如果是实际内容,我怎样才能实现 CONFIG1CONFIG3 实际上调用不同的专业化?

CONFIG1和CONFIG3是同一类型。如果有疑问,可以用std::is_same

来验证

CONFIG1CONFIG2是不同的模板实例化,CONFIG1CONFIG3不是(假设abcxyz).

完全不同
  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?

CONFIG1CONFIG3 是相同的类型。 typedef 命名无关紧要。

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

你不能,typedef 只是提供别名而不是新类型。

使用(空的)公共继承 class,实际创建一个新类型。

CONFIG1CONFIG3 是同一类型,因此您的专业化将失败并显示

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

您可以使用继承来创建新类型:

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

实例:https://ideone.com/4GrlaW