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>;
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.
5. Which keyword is used for the template? Explanation: C++ uses template reserved keyword for defining templates.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With