C++ 从其他专用函数调用专用函数
我有个错误C++ 从其他专用函数调用专用函数,c++,templates,visual-c++,C++,Templates,Visual C++,我有个错误 C2910: 'TEMPLATE_TEST::FuncTemplateTest::InnerFunc' : cannot be explicitly specialized, 在编译下面的代码时。有两个模板函数,它们都是专用的。当我在专用的外部函数中删除对InnerFunc的调用时,一切都正常工作。那么,问题在哪里?(我使用的是MS VS 2008。) 类FuncTemplateTest{ 公众: 模板 const int OuterFunc(const T&key)const;
C2910: 'TEMPLATE_TEST::FuncTemplateTest::InnerFunc' : cannot be explicitly specialized,
在编译下面的代码时。有两个模板函数,它们都是专用的。当我在专用的外部函数中删除对InnerFunc
的调用时,一切都正常工作。那么,问题在哪里?(我使用的是MS VS 2008。)
类FuncTemplateTest{
公众:
模板
const int OuterFunc(const T&key)const;
私人:
模板
const int InnerFunc(const T&key)const;
};
模板
inline const int FuncTemplateTest::OuterFunc(const T&key)const
{
std::cout我认为问题的原因是,在OuterFunc
的代码中已经使用了特定的专门化之后,您为InnerFunc
定义了一个显式专门化
如果将InnerFunc
的定义移到OuterFunc
的定义之前,则应该可以。(在GCC上,这确实解决了问题。)
另请注意:您函数的返回类型是const int
,这不是不正确的,但也很无用(const
在通过复制返回基本数据类型时被忽略)。@Aleksey\u我很高兴能帮到您,欢迎使用Stackoverflow!如果答案提供了您想要的信息,请“接受”它(通过单击其左侧的“接受”符号完成)。
class FuncTemplateTest {
public:
template<typename T>
const int OuterFunc(const T& key) const;
private:
template<typename T>
const int InnerFunc(const T& key) const;
};
template<typename T>
inline const int FuncTemplateTest::OuterFunc(const T &key) const
{
std::cout<<"Outer template\n";
return InnerFunc(key);
}
template<>
inline const int FuncTemplateTest::OuterFunc<std::string>(const std::string &key) const
{
std::cout<<"Outer special\n" << key << '\n';
InnerFunc(key); //remove this line to compile!!!
return 1;
}
template<typename T>
inline const int FuncTemplateTest::InnerFunc(const T &key) const
{
std::cout << "Inner template\nTemplate key\n";
return 0;
}
template<>
inline const int FuncTemplateTest::InnerFunc<std::string>(const std::string &key) const
{
std::cout << key << '\n';
return 1;
}