tags:

views:

133

answers:

2

A non-exported template that is neither explicitly specialized nor explicitly instantiated must be defined in every translation unit in which it is implicitly instantiated; no diagnostic required.

Can any one explain this?

actual point is from ISO standard 14th chapter 14.0 ,point 8

+4  A: 

So basically, for most intents and purposes, a "translation unit" is going to be a source file (usually a .cpp or .cc file). What this is saying is that if you don't do something that explicitly instantiates the template, then every TU that wants to use it, must have the template in it.

In this end, this usually equates to "put your templates in .h files and include that in every source file that needs the template".

Evan Teran
+1  A: 

The thing with implicitly instantiated template is that the compiler doesn't know whether you actually meant to use a template function or a non-template function (since the instantiation is implicit)

If in the translation unit (that is , a source file after pre-compiling steps was done) you call a function without explicitly using template parameters,the compiler:

  1. Check in the translation unit whether there is non template function which comply with the function call.
  2. if such not exist , check translation unit if there is a template function which comply with the function call.
  3. if such not exist ... compilation error

So , that means that if there is a function call which you want it to link with some kind of template instantiation , the template definition must be in the same translation unit ,the compiler won't go and check it in other Translation units (unless you use export keyword , and even in that case , I am not sure how many compilers support this bizarre feature)