For the below code, I got some compiling errors and don't know why.
~/sandbox/test-cxx $ g++ test-template-specialization.cpp
test-template-specialization.cpp:23:12: error: invalid use of incomplete type ‘class Search_A_Scale<A, 0, C>’
operator()()
^
test-template-specialization.cpp:8:7: error: declaration of ‘class Search_A_Scale<A, 0, C>’
class Search_A_Scale
^
Code:
/* test-template-specialization.cpp */
#include <iostream>
#include <string>
using namespace std;
template <int A,
int B,
int C>
class Search_A_Scale
{
public:
bool operator()();
enum {
kb = B,
kc = C
};
};
#if 1 // this block cimpiled failed.
template <int A,
int C>
bool Search_A_Scale<A, 0, C>::
operator()()
{
return true;
}
#else // this block compiled successfully.
template <int A, int C>
class Search_A_Scale<A, 0, C>
{
public:
bool operator() () { return true; }
};
#endif
int main()
{
Search_A_Scale<1, 0, 24> a;
cout << a() << endl;
return 0;
}
UPDATED: My own answer
According the standard 14.5.5.3
A class template specialization is a distinct template. The members of the class template partial specialization are unrelated to the members of the primary template.
That is to say the primary class template Search_A_Scale<A, B, C>
is distinct with the class template partial specialization Search_A_Scale<A, 0, C>
, so the specified member Search_A_Scale<A, 0, C>::operator()
requires a definition of the Search_A_Scale<A, 0, C>
but which is missed.