c++ - 成员模板特化

标签 c++ templates template-specialization partial-specialization

template<typename T>
class C
{
   void f() { }
};

/*template<typename T>
void C<T*>::f() { }*/

template<>
void C<int*>::f() { }

如果我们删除注释,代码将无法编译。我知道这一点(而且我也知道,我们应该有 partial specialization 代表 C<T*>),但我找不到标准的词来解释这种行为。我重读了14 par标准的几次。你能给我一个报价或部分标准来解释这个吗?

编辑。

template<typename T>
class C
{
   template<typename U>
   struct S { };
};
// #1
/*template<typename T>
class C<T*>
{
   template<typename U>
   struct S { };
};*/
// #2
/*template<typename T>
template<typename U>
struct C<T*>::S<U*> { };*/

template<>
template<typename U>
struct C<int*>::S<U*> { };

如果我们接下来只删除注释,那么 #2 - 代码将无法编译。

最佳答案

这是从 14.7.3/1 开始明确专门化的内容的标准引用:

An explicit specialization of any of the following:

— function template

— class template

member function of a class template

— static data member of a class template

— member class of a class template

— member enumeration of a class template

— member class template of a class or class template

— member function template of a class or class template

can be declared by a declaration introduced by template<>;

除非明确允许,否则您不能部分特化任何东西,并且不明确允许类模板的成员函数。只有类模板可以部分特化(如 14.5.5 中所述)。

(请注意,显式特化类模板的成员类模板本身就是一个类模板。)

关于c++ - 成员模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12340105/

相关文章:

c++ - 是否需要实现来诊断对同一TU中相同显式专业的重复定义进行ODR违规?

c++ - 为提升精神编译自定义容器时出错

c++ - 拥有指向保留 vector 元素的指针是否合法?

c++ - 是否有一种巧妙的方法来避免覆盖模板基类的所有纯虚函数,用于多重继承

templates - 当 <template if ='...' > 最终实例化为 Dart Polymer 中的 DOM 时如何设置回调

c++ - 如何使用模板模板参数来专门化类模板?

c++ - 两个数相乘是常数时间算法吗?

c++ - 在函数参数未定义行为中使用赋值运算符吗?

c++ - dynamic_cast 到具有未知模板参数的派生类型

c# - 通用索引器重载特化