4

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?

Lio
  • 87
  • 7

1 Answers1

5

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.)

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • Thanks, that was plain and easy after your comment. I'm however getting a compiler-dependent linking time error due to the `template class` not instantiating an explicitly defaulted move constructor... This seems to be a known bug of some compilers, [see here](http://stackoverflow.com/questions/38929950/automatic-constructor-in-explicitly-instantiated-class-template) in case it helps someone. – Lio Jan 28 '17 at 13:37