I have an error
C2910: 'TEMPLATE_TEST::FuncTemplateTest::InnerFunc' : cannot be explicitly specialized,
while compiling the code below. There are two template functions, and both of them are specialized. When I remove the call to InnerFunc in the specialized outer one, everything works normally. So, where is the problem? (I’m using MS VS 2008.)
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;
}
I believe the cause of the problem is that you define an explicit specialization for
InnerFuncafter that particular specialization has already been used in the code forOuterFunc.If you move the definitions for
InnerFuncbefore the definitions forOuterFunc, you should be fine. (On GCC this indeed solved the problem.)Separate note: The return type of your functions is
const int, which is not incorrect, but also quite useless (constis ignored when fundamental data types are returned by copy).