Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "template" keyword doing before "class" keyword?

Tags:

c++

templates

Just some code sample [Not a real life example]

// at file scope

template <typename T, typename U>
struct demo{};  
template class demo<int, int>; // is the template keyword optional here?

Is the template keyword optional in line 3? I haven't (often) seen this usage of the template keyword before. Which part of the standard says allows this?

EDIT

I think g++ has a bug then.

template <typename T, typename U>
struct demo{};  
class demo<int, int>; // template keyword omitted

compiles on g++ (4.5.1) whereas fails on Comeau

"ComeauTest.c", line 5: error: specializing class "demo<int, int>" without
          "template<>" syntax is nonstandard
      class demo<int, int>; 
like image 638
Prasoon Saurav Avatar asked Apr 24 '11 06:04

Prasoon Saurav


People also ask

What is the template keyword in C++?

C++ adds two new keywords to support templates: 'template' and 'typename'. The second keyword can always be replaced by the keyword 'class'. How Do Templates Work? Templates are expanded at compiler time.

What keywords use templates?

5. Which keyword is used for the template? Explanation: C++ uses template reserved keyword for defining templates.

What is template What is the need of template declare a template class?

Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Generic Programming is an approach to programming where generic types are used as parameters in algorithms to work for a variety of data types.In C++, a template is a straightforward yet effective tool.

What is the difference between class template and function template?

For normal code, you would use a class template when you want to create a class that is parameterised by a type, and a function template when you want to create a function that can operate on many different types.


1 Answers

That's an explicit instantiation.

Typically when you use a template, the compiler generates what you need, as you need it. To provide essential specializations of a class template in a static or dynamic library, however, you want to generate all the members, all at once, to make sure they are delivered to the user.

For example, most implementations of the C++ standard library explicitly specialize std::ostream<char,char_traits<char> >, because otherwise applications would end up including repetitive copies of various operations on cout.

This syntax is identical with explicit instantiation. C++03 §14.7.2/2:

The syntax for explicit instantiation is:

explicit-instantiation:

template declaration

Edit:

It looks like you stumbled on obsolete syntax for specializing, not explicitly instantiating, a class template. Comeau is warning you that it took the template-id declaration as a forward declaration of an explicit specialization. Presumably GCC is doing the same. It is unlikely that you are obtaining explicit instantiation in that case. Also, it is undefined behavior to use an explicit template specialization before it is defined. (Fundamentally, the implicit specialization causes a violation of the one-definition rule.)

Note that GCC also supports extern template instantiations:

extern template declaration

In the case of an extern function instantiation, I would not be surprised if template were optional. But, neither would I be surprised to find it is required, nor omit it.

like image 147
Potatoswatter Avatar answered Oct 26 '22 23:10

Potatoswatter