Search code examples
c++templatescompilationexplicit-instantiation

Clang/g++ options for template instantiation


I'm looking for a compiler option on g++/clang++ to control the instantiation of methods in explicit instantiations.

Suppose I have a classtemplate Foo<T> with some explicit instantiation

template Foo<int>;

I want to enable the instantiation of all public methods. This doesn't seem to be the default, as I'm having to add manual instantiations for all the members in order to avoid link errors;

template void Foo<int>:DoStuff(int);
template int Foo<int>:GetSomeStuff();

Solution

  • You can either instantiate each method individually or you would instantiate all of them for the class. It seems you want to do the latter but the notation you tried is wrong. The correct one is this:

    template class Foo<int>;
    

    (assuming Foo is declared as a class; if it is declared as a struct you'd use the struct keyword instead).