gpt4 book ai didi

c++ - 如何专门化模板类中的模板成员函数(已经专门化)?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:49:58 24 4
gpt4 key购买 nike

例如:

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>::Fun()

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. */
}

关于c++ - 如何专门化模板类中的模板成员函数(已经专门化)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17503697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com