Lio
Lio

Reputation: 87

Extern template: declaration does not declare anything

I have a template class

expof.h:

template <class T>
class ExpOf{
...
}

which I repeatedly use throughout my code for e.g. T = double [and other classes that ExpOf should not know anything about]. So I thought it would be a good idea to compile it once and for all [or twice...]

expofdouble.cpp:

#include "expof.h"
template class ExpOf<double>;

and declare it in a different header so it would not get compiled when expof.h is included.

expofdouble.h:

extern template ExpOf<double>;

When I compile this (clang-800.0.42.1) I get (numerous) warnings

expofdouble.h: warning: declaration does not declare anything [-Wmissing-declarations]
extern template ExpOf<double>;
                ^~~~~~~~~~~~~

Am I getting the desired behavior? Why the warning then? Should I do this differently?

Upvotes: 4

Views: 2682

Answers (1)

cdhowie
cdhowie

Reputation: 168998

expofdouble.h should contain this line:

extern template class ExpOf<double>;

Your declaration omits the class keyword, so it doesn't actually declare anything.

(Note that you'll get the same warning with a declaration like extern int;, which quite obviously doesn't do anything useful for you.)

Upvotes: 5

Related Questions