1

I am reading on type erasure:

Andrzej's C++ blog Type erasure — Part I

Where I come across the following text:

unless you can enumerate all instantiations of your template in advance, you have to include the body of each function template in the header file, you cannot separate the declaration from the implementation

Is enumerating all instantiations of the template the same as explicit instantiation pointed out in answer to the following question?

Why can templates only be implemented in the header file?

Another solution is to keep the implementation separated, and explicitly instantiate all the template instances you'll need:

// Foo.h

// no implementation
template <typename T> struct Foo { ... };

//----------------------------------------    
// Foo.cpp

// implementation of Foo's methods

// explicit instantiations
template class Foo<int>;
template class Foo<float>;
// You will only be able to use Foo with int or float
Vinod
  • 925
  • 8
  • 9
  • @L.F. edited the original question – Vinod Jul 30 '19 at 07:32
  • 1
    OK. Yeah, the blogger is apparently referring to explicit instantiation. You have to know all specializations to be used in advance in order to use explicit instantiation to separate implementation. – L. F. Jul 30 '19 at 07:34
  • 1
    Possible duplicate of [When would you use template explicit instantiation?](https://stackoverflow.com/questions/13068993/when-would-you-use-template-explicit-instantiation) – L. F. Jul 30 '19 at 08:31
  • 1
    Enumerating all instantiations of a template is something the developer does to create a list of those instantiations. Having that list is necessary to be able to explicitly instantiate the template. – Peter Jul 30 '19 at 08:46

1 Answers1

0

Mostly yes.

What it boils down to is "can you know everywhere this is used?". The authors of std::vector<T> can't know all the types T will be substituted for.

That's the "enumerate all instantiations" step, which is followed by "write down all the explicit instantiations".

Caleth
  • 52,200
  • 2
  • 44
  • 75