2

C++11 introduced a feature called 'extern template' which indicates that template instance exists in other translate unit.(Am I right?)

This(http://www.youtube.com/watch?v=3annCCTx35o) lecture also tells that if you specify extern template and don't include instantiation, the linker will produce error.(around 2:25 in the video)

So, I've tried to build next code:

#include <iostream>

template<class T>
struct Foo
{
    static constexpr int type_size = sizeof(T);
};

extern template struct Foo<int>;

int main()
{
   std::cout<< Foo<int>::type_size << std::endl;
   return 0;
}

I expected the build will fail because this file does not contain explicit instantiation nor specialization, but gcc just builds it up the result runs well.

What am I missing? Or, am I misunderstanding something? Or, does not gcc support extern template well?

Update

I've tried a class with non-inline function, and extern template works as expected!

#include <iostream>

template<class T>
struct Foo
{
    static void print(T t);
};

template<class T>
void Foo<T>::print(T t) { std::cout << t << std::endl; }

extern template struct Foo<int>;

// template struct Foo<int>;

int main()
{
   Foo<int>::print(1);
   return 0;
}

Above source is not built without the commented line. Thank you all guys!

11
  • Having trouble understanding your question... what is the problem? May 13, 2014 at 13:56
  • 3
    Try adding a (non-inline!) function to Foo, and see what happens then.
    – dlf
    May 13, 2014 at 13:57
  • 1
    I was under the impression that the GCC guys were the first to do extern template, and it was available in older versions of g++ before the new C++ standards were put together. So it might not be the compiler at fault here!
    – Rook
    May 13, 2014 at 14:00
  • @dlf Okay, my problem was that all functions were inlined! Thank you.
    – slyx
    May 13, 2014 at 14:07
  • 2
    Just for completeness: When you ask a question of the form "Feature X does not work on compiler Y" you should mention which version of the compiler you are using. In this case it wasn't the issue, but there are many cases in which the answer would be "Feature X was added in Compiler Y version nnnn" May 13, 2014 at 15:03

1 Answer 1

3

if you specify extern template and don't include instantiation, the linker will produce error.

No, not necessarily. There is only a problem if you actually use the template. You're using a compile-time constant defined as a static member of that template, but that is replaced by the constant's value at compile-time. And after that replacement, there is no longer any use of the template, so there is no need for a definition of the template.

2
  • Adding Foo<int> foo; in main still compiles. May 13, 2014 at 14:08
  • 2
    @PeterSchneider Right, but that too doesn't use any part of the template that isn't directly available at compile-time. (In particular, it doesn't call any constructor or destructor, because Foo's constructor and destructor are trivial.)
    – user743382
    May 13, 2014 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.