7

There are two cases where typedef confuses me when it comes to extern template declaration and explicit template instantiation.

To illustrate the two see below 2 example code snippets.

Consider following example (Case 1):

// suppose following code in some cpp file    
template <typename T>
    struct example
{
    T value;
};

// valid typedefs
typedef example<int> int_example;
typedef example<std::string> string_example;

// explicit instantiation using above typedefs
template class int_example; // -> compile time error
template class string_example; // -> compile time error

// instead we need to use type names
template class example<int>; // -> OK
template class example<std::string>; // -> OK

// QUESTION 1: Why does this work however? is this valid code?
typedef std::string type_string;
template class example<type_string>;

Why does the template class example<type_string> work with typedef ? and why is it valid while template class string_example is not?

Consider following example (Case 2):

// suppose following code is in some header file
template <typename T>
struct example
{
    T value;
};

// valid typedefs
typedef std::string type_string;
typedef example<type_string> string_example;

// Explicit instantiation declaration
// QUESTION 2: Is this valid code? if not why not?
extern template string_example; // -> at least this compiles, but is it OK?

As questioned in the comment above, is it valid to use typedef in extern template declaration, like in the example above, and why does this compile unlike the Case1 where it does not.

I've read about similar cases but none gives the detailed answer to above 2 questions. detailed elaboration is very much appreciated!

codekiddy
  • 5,897
  • 9
  • 50
  • 80

1 Answers1

3
template class int_example;

is not legal. From the C++11 Stanard:

14.7.2 Explicit instantiation

2 The syntax for explicit instantiation is:

explicit-instantiation:
externopt template declaration

There are two forms of explicit instantiation: an explicit instantiation definition and an explicit instantiation declaration. An explicit instantiation declaration begins with the extern keyword.

3 If the explicit instantiation is for a class or member class, the elaborated-type-specifier in the declaration shall include a simple-template-id.

simple-template-id is defined in Section A.12 Templates as:

simple-template-id:
template-name < template-argument-listopt >

int_example does not qualify as a simple-template-id.
example<int> does qualify as a simple-template-id.

However, by that logic,

extern template string_example;

is not legal either. I don't know how it works for you. I got the following error when I tried to compile such a line in g++ 4.9.3.

socc.cc:15:31: error: expected unqualified-id before ‘;’ token
 extern template string_example; // -> compile time error
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • do you know why `template class example;` and `extern template class example;`works (or it does not with GCC), I'm using MSVC-140 btw and it works. thanks so much! – codekiddy Dec 29 '15 at 08:13
  • `example` qualifies as a *simple-template-id*. – R Sahu Dec 29 '15 at 18:00
  • thanks, I've done some tests with GCC and there are cases where g++ issues undefined references for cases such as `example` but msvc compiles just fine, there are also cases where msvc gives warnings but not g++, obviously this cases are neither described nor well defined by standard. – codekiddy Dec 30 '15 at 10:02