Assume I have a class Module<Setting>
that I want to instantiate externally. (reducing compilation time...)
I created a Module.cpp
that contains template class Module<Setting>;
and added the extern
declaration in the header. This works fine.
Now I want to wrap this Module
instantiation in another class, for example like this:
template<template<typename> class M, typename First>
struct TrivialInstantiator {
typedef M<First> type;
};
This does not seem to work, i.e. the compilation time for other compilation units does not change (in comparison to not using the extern declaration at all). My question: Why is that so? Can I somehow make it work? (I tried to read the relevant sections of the standard, but I couldn't quite figure it out...)
Rationale for my question:
I have a larger number of Settings, and people may create new Settings that are not familiar with these details. If I simply create a list of extern template declarations for Module<Setting1>
, Module<Setting2>
, etc., this is not only tedious but will also tend to produce errors when users forget to add the template instantiation in the cpp file for example.
Therefore I tried to create a list of Settings (with boost::mpl
) at one point and use this list of Settings to instantiate all of them at once.
Edit: This other question is about how extern works altogether, it's not about how it handles nested types or typedefs.
Update: I read §14.7.2.8 several times again:
An explicit instantiation that names a class template specialization is also an explicit instantiation of the same kind (declaration or definition) of each of its members (not including members inherited from base classes and members that are templates) [...].
I tried the following:
template<template<typename> class M, typename First>
struct TrivialInstantiator {
M<First> member;
};
As this does not work either: Is this member considered a template as in not including members [...] that are templates? (Isn't it a fully instantiated template that is something different...? Is there any way to workaround that?)