3

I have a template method in class:

template<class T>
    A& doSomething(T value)

Then I have an implementation

template<class T>
    A& A::doSomething(T value){
        // do something with value
        return *this;
    }

Then I have a specialization for lets say bool

template<>
    A& A::doSomething(bool value){
        // do something special with value of type bool
        return *this
    }

This is what I understand, now in the code there is something like this which I don't know what mean:

template A& A::doSomething(char const*);
template A& A::doSomething(char);
template A& A::doSomething(int);
template A& A::doSomething(int*);
template A& A::doSomething(double);
...

What is the exact meaning of those last 5 lines with template?

Thanks

Jan
  • 1,054
  • 13
  • 36

2 Answers2

5

It's Explicit instantiation, which forces the instantiation of the templates with the specified template arguments, and prevents their implicit instantiations.

An explicit instantiation definition forces instantiation of the function or member function they refer to. It may appear in the program anywhere after the template definition, and for a given argument-list, is only allowed to appear once in the program.

An explicit instantiation declaration (an extern template) prevents implicit instantiations: the code that would otherwise cause an implicit instantiation has to use the explicit instantiation definition provided somewhere else in the program.

And see Explicit instantiation - when is it used?


Because you tagged c++03, please note that extern template mentioned in the linked page was introduced from c++11.

Community
  • 1
  • 1
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
2

This lines aren't implementation. There only say to compiler to "create" code for doSomething for types char const*, char, int, int * and double

template A& A::doSomething(char const*);
template A& A::doSomething(char);
template A& A::doSomething(int);
template A& A::doSomething(int*);
template A& A::doSomething(double);
Jarod42
  • 203,559
  • 14
  • 181
  • 302
Garf365
  • 3,619
  • 5
  • 29
  • 41