Domanda

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

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top