ambiguous behaviour of a class template with specialised function

I have got the following code in my header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <class TType>
class CTestTmpl
{
public:
           CTestTmpl() {};
          ~CTestTmpl() {};
   void    Get() const;
private:
};

template <class TType>
void
CTestTmpl<TType>::Get() const
{
      std::cout << "This is not specialized: " << std::endl;
}


Then, I have the following specialised function:
1
2
3
4
5
6
    template <>
    void
    CTestTmpl<int>::Get() const
    {
          std::cout << "This is a specialized implementation: " << std::endl;
    }


If I put this specialised function in a ".cc" file, and then call it from some main function, like this:
1
2
3
4
5
6
7
    int main()
    {
          CTestTmpl<int> intTmpl;   
          intTmpl.Get();
          CTestTmpl<float> floatTmpl;   
          floatTmpl.Get();
    }

it works as expected: The "intTmpl" uses the specialised function, and the "floatTmpl" uses the general function implementation. (The output is respectively: "This is a specialized implementation: " and "This is not specialized: ".)
However, if I put the CTestTmpl.o file in a lib, link to it from a different program, and execute the same code, both "intTmpl" and "floatTmpl" use the general function implementation. (The output is for both: "This is not specialized: ".)

On the other hand, if I put the specialised function in the header file, it will not compile in the "local code" (it complains the function is defined twice), but, when I compile only the class and put it in a lib, and link to it from the different program, now "intTmpl" correctly uses the specialised implementation and "floatTmpl" the general implementation. (With output respectively: "This is a specialized implementation: " and "This is not specialized: ".)

Could anybody explain to me what's going on here, please?
Where should I put the specialised function?
And, once I have specialised a function for one type (integer), do I have to specialise it for all types (float, double, ...)?

Best regards,

Machiel


Some people on an other forum just gave me the solution.

When adding the following to the header file (aka "explicit instantiation"), the code works fine when linking to in a lib:
1
2
3
template <>
void
CTestTmpl<int>::Get() const;
Topic archived. No new replies allowed.