Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Explicit Template Instantiation Declaration doesn't avoid code generation.

Even_E_
New Contributor I
559 Views

Hello,

Here is a proof of concept :

template <typename T>
struct zoo
{
    T a;
    T foo() { return 2 * a; } // at least one function so zoo requires some code generation.
                              // -> explicit decl are more likely to require an explicit def.
};

extern template zoo<int>;

int main()
{
    zoo<int> a;
    return a.foo();
}

This single source file program compiles and links without error with "Intel C++ 15.0" on Windows, while in my understanding it would be looking for a missing explicit definition.

So does it mean compilation isn't faster using the new C++11's "extern template" method ?

And how can I know I'm not missing any explicit definitions in my library except by trying to compile with a conformant compiler ?

Thank you in advance.

0 Kudos
3 Replies
Judith_W_Intel
Employee
559 Views

 

I assume you meant to use "extern template class zoo<int>;" not "extern template zoo<int>;".

Since you declared the routine inline, the compile is inlining the call. GNU will do the same thing at higher optimization levels, i.e. -O1 and higher.

If you want to get link errors move the definition of the routine out of line, i.e.:

sptel15-253> cat extern.cpp

template <typename T>
struct zoo
{
  T a;
  T foo();
//{ return 2 * a; }
};

// out of line definition
template <typename T>
T zoo<T>::foo() { return 2 * a; }

extern template class zoo<int>;

int main()
{
  zoo<int> a;
  return a.foo();
}

sptel15-254> icc48 extern.cpp
/tmp/iccXTR1CV.o: In function `main':
extern.cpp:(.text+0x30): undefined reference to `zoo<int>::foo()'
sptel15-255>

See:

http://stackoverflow.com/questions/6726327/does-extern-template-prevent-inlining-of-functions

 

 

 

Judy

0 Kudos
Even_E_
New Contributor I
559 Views

Thanks for the hint on inlining.

So if I understand well, since the choice of inlining - or not - depends on the ability of the definition to respect some operation pattern or size, I expect that the compiler does at least half the job in terms of code generation for every inline definitions. Thus, for the extern mechanism to be really effective in release, one needs to also put definitions out of line.

P.S.: I just found out that extern explicit instantiation of members actually prevent code generation.

extern template int zoo<int>::foo();
error LNK2019: unresolved external symbol

So I now really think that the extern instantiation of the class should prevent every code generation, that way we would have total and easy control over it.

 

0 Kudos
Judith_W_Intel
Employee
559 Views

 

Inlining depends on lots of things -- whether the function is declared inside or outside the class, the optimization level, explicit inline flags, attributes/declspecs, etc.

extern template always prevents code generation (i.e. if you look you'll see that the function is not present in the object file) but the way you are testing to see if it's there (by calling it) is inlined so you get no unresolved link error.

Judy

0 Kudos
Reply