Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template extern (vs extern template)

I've ran across a code like this:

template<class T> extern void f(T a);

in the .h file, and f() is defined with several specific T's in the cpp file.

I couldnt find any explanation about this syntax. I've seen that extern template was introduced in c++11 but they seem to have different syntax...

Can someone elaborate?

Thanks

like image 606
Mark S Avatar asked May 15 '14 13:05

Mark S


1 Answers

The extern is superfluous here. It means that functions instantiated from this template have external linkage. It's the normal extern you'd apply to an ordinary function definition to indicate it has external linkage. Which is also superfluous, since functions have external linkage by default. So it's the same as

template <class T> void f(T a);

You've mentioned that several explicit instantiations (or maybe specialisations, your question's not clear on this) are defined in the .cpp file. This still has nothing to do with the extern keyword being used, and could be done without it as well.

like image 135
Angew is no longer proud of SO Avatar answered Sep 22 '22 04:09

Angew is no longer proud of SO