如何在模板类中专门化模板成员函数(已指定)
How to specialize template member functions in template class (already specilzied)?
例如:
template<unsigned number>
struct A
{
template<class T>
static void Fun()
{}
};
template<>
struct A<1>
{
template<class T>
static void Fun()
{
/* some code here. */
}
};
并且想要专门化A<1> ::函数()
template<>
template<>
void A<1>::Fun<int>()
{
/* some code here. */
}
似乎不起作用。怎么做?谢谢
类模板的显式专用化类似于常规类(它是完全实例化的,因此不是参数类型)。因此,您不需要外部template<>
:
// template<> <== NOT NEEDED: A<1> is just like a regular class
template<> // <== NEEDED to explicitly specialize member function template Fun()
void A<1>::Fun<int>()
{
/* some code here. */
}
类似地,如果您的成员函数Fun
不是一个函数模板,而是一个常规成员函数,那么您根本不需要任何template<>
:
template<>
struct A<1>
{
void Fun();
};
void A<1>::Fun()
{
/* some code here. */
}
相关文章:
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何基于模板化类的基类专门化成员函数
- 专门化模板覆盖函数/避免对象切片
- 如何使用模板化类专门化模板化函数?
- 线程 std::调用未知类型,无法专门化函数错误
- 如何在编译时专门化大型模板函数中的小部分
- 模板函数,其中一个参数需要专门化,而另一个不需要
- 错误 C2893 无法专门化函数模板'unknown-type std::invoke(_Callable &&,_Types &&...)'
- 从 std 命名空间中专门化函数模板的想法有多糟糕?
- 为模板函数专门化一个模板类
- 基于参数值的c++模板成员函数专门化
- 如何确定函数专门化的主模板
- 等价参数的c++函数专门化
- 基于运行时值调用不同的模板函数专门化
- 跨库的模板函数专门化符号匹配
- 模板函数专门化,c++
- 如何使用type_traits或模板函数专门化来整合模板方法
- 如何在子类中重载模板化函数(专门化)
- 可变模板函数:专门化头/尾和空基准情况
- 不允许void的部分函数专门化-替代解决方案