c++ - 模板的总类特化

标签 c++ templates special-characters template-specialization

假设我有一个模板类

template <typename T>
struct Widget
{
   //generalized implementation
}

但我想完全专攻.. 对于接受参数的模板?

template <>
struct Widget< TemplateThatAcceptsParameter<N> >
{
       //implementation for Widget for TemplateThatAcceptsParameterN 
       //which takes parameter N
}

如何做到这一点?

最佳答案

这称为部分特化,可以这样编码:

template <typename T>
struct Widget
{
   //generalized implementation
};

template <typename N>
struct Widget< TemplateThatAcceptsParameter<N> >
{
   //implementation for Widget for TemplateThatAcceptsParameterN 
   //which takes parameter N
};

它的工作方式与常规特化一样,但有一个额外的模板参数。

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

相关文章:

c++ - 在 header 中的宏中初始化结构体,必须在 C 和 C++ 源代码中工作

django - 获取模板中的模型字段名称

c++ - 如何告诉编译器我的 friend 函数是函数模板

syntax - #在LISP中是什么意思

c++ - 在 C++ 中将输出写入文件的问题

c++ - 将数据加载到结构 vector 中

c++ - “引用类型 const..”在索引 2d vector 时出现编译错误

c++ - 有没有办法将输入类型包装到 C++ 中的模板类?

c# - C# TCP 服务器和 Java TCP 客户端之间的编码问题

Android:如何使用Roboto特殊字符?