Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template method implementation without <>

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

like image 973
Jan Avatar asked Sep 28 '26 01:09

Jan


1 Answers

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.

like image 82
songyuanyao Avatar answered Sep 30 '26 15:09

songyuanyao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!