PAYATHON 2026

类模板成员类模板的偏/全特化为何产生冲突输出?

数据小陈

结论

按 C++ 标准,这段程序合法,预期输出:

0 1 1 0
0 1 2 3
9 9 9 9

其中 A<long>::B<long&>::value == 3;A<short>::B<int*>、A<short>::B<void*>、A<short>::B<long&> 都应为 9。如果某个编译器给出不同结果,应先怀疑编译器的嵌套类模板特化实现或语言模式配置,而不是模板本身有 UB。只有特化声明出现在首次隐式实例化之后等情形,才可能变成 ill-formed, no diagnostic required;本例中所有特化都在 main 使用前声明。

规则怎么理解

  • 匹配顺序是:显式特化优先于偏特化,偏特化优先于主模板。
  • A<short>::B 这种写法:
template <>
template <typename U>
struct A<short>::B { static constexpr int value = 9; };

它显式特化了 A<short> 的成员模板 B 的主模板。对 A<short> 来说,原来的偏特化 A<T>::B<U*> 不再参与匹配,所以所有 A<short>::B<...> 都得到 value = 9。

  • A<long>::B<void*> 是成员模板 B<void*> 的显式特化,只覆盖 void* 这一个实参,不会替换 A<long> 下的整个 B 模板。
  • A<long>::B<U&> 是给 A<long> 的成员模板 B 新增的偏特化,用来匹配引用类型。所以 A<long>::B<long&> 命中它,得到 3;A<long>::B<int*> 仍命中原来的 B<U*>,得到 1。

逐项结果

  1. A<char>:没有其他特化。int 走主模板,得到 0;int* 匹配 B<U*>,得到 1;void* 也匹配 B<U*>,得到 1;long& 不匹配任何偏特化,走主模板,得到 0。
  2. A<long>:int 走主模板,得到 0;int* 匹配 B<U*>,得到 1;void* 命中显式特化,得到 2;long& 命中 B<U&>,得到 3。
  3. A<short>:成员模板 B 的主模板已被显式特化,所有实参都得到 9。

可验证代码

#include <iostream>

template <typename T>
struct A {
  template <typename U>
  struct B { static constexpr int value = 0; };

  template <typename U>
  struct B<U*> { static constexpr int value = 1; };
};

template <>
template <>
struct A<long>::B<void*> { static constexpr int value = 2; };

template <>
template <typename U>
struct A<long>::B<U&> { static constexpr int value = 3; };

template <>
template <typename U>
struct A<short>::B { static constexpr int value = 9; };

static_assert(A<char>::B<int>::value == 0);
static_assert(A<char>::B<int*>::value == 1);
static_assert(A<char>::B<void*>::value == 1);
static_assert(A<char>::B<long&>::value == 0);

static_assert(A<long>::B<int>::value == 0);
static_assert(A<long>::B<int*>::value == 1);
static_assert(A<long>::B<void*>::value == 2);
static_assert(A<long>::B<long&>::value == 3);

static_assert(A<short>::B<int>::value == 9);
static_assert(A<short>::B<int*>::value == 9);
static_assert(A<short>::B<void*>::value == 9);
static_assert(A<short>::B<long&>::value == 9);

int main() {
  std::cout << A<char>::B<int>::value << ' '
            << A<char>::B<int*>::value << ' '
            << A<char>::B<void*>::value << ' '
            << A<char>::B<long&>::value << std::endl;

  std::cout << A<long>::B<int>::value << ' '
            << A<long>::B<int*>::value << ' '
            << A<long>::B<void*>::value << ' '
            << A<long>::B<long&>::value << std::endl;

  std::cout << A<short>::B<int>::value << ' '
            << A<short>::B<int*>::value << ' '
            << A<short>::B<void*>::value << ' '
            << A<short>::B<long&>::value << std::endl;
}

预期输出:

0 1 1 0
0 1 2 3
9 9 9 9

注意事项

  • 显式特化和偏特化必须在首次使用所触发的隐式实例化之前可见。把 A<long>::B<void*> 或 A<short>::B 放到 main 后面,可能变成 IFNDR。
  • 用较新的 GCC/Clang 并加 -std=c++17 -pedantic-errors;MSVC 可以打开 /permissive-。这些选项有助于暴露非标准行为。
  • 如果希望 A<short> 保留指针偏特化,就不要显式特化整个成员模板 B;应改为针对具体 B<...> 的显式特化,或保留 A<T>::B<U*> 参与匹配。
  • 嵌套成员模板特化是历史实现差异较多的区域。遇到跨编译器不一致时,先用 static_assert 固定每个分支的预期,再检查编译器是否符合标准。

备注:内容仅供参考。