【发布时间】:2015-03-13 20:55:49
【问题描述】:
假设我在 Visual Studio 中有以下代码
class foo
{
public:
template<typename t>
void foo_temp(int a , t s_)
{
std::cout << "This is general tmeplate method";
}
template<>
static void foo_temp(int a , int s)
{
std::cout << "This is a specialized method";
}
};
int main()
{
foo f;
f.foo_temp<std::string>(12,"string");
}
现在我正试图将其转换为 GCC。通过关于 SO 的其他问题,我注意到如果类不是专门的,则在 GCC 成员方法中不能专门化。因此我想出了这个解决方案
class foo
{
public:
template<typename t>
void foo_temp(int a , t s_)
{
std::cout << "This is general template method";
}
};
template <>
/*static*/ void foo::foo_temp<int>(int a, int value) {
std::cout << "Hello world";
}
现在这似乎可以解决问题,但是当我在语句中包含 static 关键字时,我得到了错误
explicit template specialization cannot have a storage class
现在this 线程谈论它,但我仍然对如何在这里应用它感到困惑。关于如何使最后一个方法成为静态的任何建议?此外,我仍然对为什么 GCC 中的模板方法不能是静态的感到困惑?
这是视觉工作室代码
class foo
{
public:
template<typename t>
void foo_temp(int a , t s_)
{
std::cout << "This is general tmeplate method";
}
template<>
static void foo_temp(int a , int s)
{
std::cout << "This is a specialized method";
}
};
int main()
{
foo f;
f.foo_temp<std::string>(12,"string");
}
【问题讨论】:
-
你为什么要这样做?
-
我正在移植一个代码,它不像我提出的那样简单。
-
说真的,在 MSVC 中编译的原始代码??!我知道它的模板引擎非常不合格,但这是一个新低。
-
@T.C.是的,VS2013 没有任何抱怨。我将
foo::foo_temp(12,12);添加到main(),但仍然没有。甚至第二个 sn-p 也被接受,static等等,但我必须将其称为f.foo_temp(12,12); -
@Praetorian 哇。哇。
标签: c++