Calling a specialized function from another specialized

I have a mistake

C2910: 'TEMPLATE_TEST::FuncTemplateTest::InnerFunc' : cannot be explicitly specialized, 

when compiling the code below. There are two template functions, and both are specialized. When I delete the InnerFunc call in a specialized external, everything works fine. So where is the problem? (I am 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; } 
+4
source share
1 answer

I believe the cause of this problem is that you define an explicit specialization for InnerFunc after a specific specialization is already used in the code for OuterFunc .

If you move definitions for InnerFunc before definitions for OuterFunc , you should be fine. (At GCC, this really solved the problem.)


A separate note: the return type of your functions is const int , which is not incorrect but also completely useless ( const ignored when the main data types are returned by the copy).

+3
source

Source: https://habr.com/ru/post/1445705/


All Articles