Reputation: 22854
Does the C++ standard
somehow specify what can a T
be in the following declaration:
template <typename T>
I mean, from practical terms of view this can be any particular type, which allows the template to compile (when the corresponding substitution happens).
But what about the strict definition?
Upvotes: 0
Views: 667
Reputation: 10903
boost has a helpful template, enable_if that lets you enable templates for only particular types.
Upvotes: 0
Reputation: 38193
As you want the standard, here it is:
C++03, 14.1, Template parameters:
A template defines a family of classes or functions.
template-declaration:
exportopt template < template-parameter-list > declaration
template-parameter-list:
template-parameter
template-parameter-list , template-parameter
template-parameter:
type-parameter
parameter-declaration
type-parameter:
class identifieropt
class identifieropt = type-id
typename identifieropt
typename identifieropt = type-id
template < template-parameter-list > class identifieropt
template < template-parameter-list > class identifieropt = id-expression
..
A type-parameter defines its identifier to be a type-name (if declared with class or typename) or template-name (if declared with template) in the scope of the template declaration.
..
If the use of a template-argument gives rise to an ill-formed construct in the instantiation of a template specialization, the program is ill-formed.
The other things are for default parameters, non-type templates, etc. In other words, the standard does not say anything about T
.
Upvotes: 2
Reputation: 12201
There is no strict definition, since that would seem to go against the very purpose of templates. T
is any type that is, for example, passed as an argument to a function with a parameter of type T
.
You sacrifice the safety of strict type definitions for the code reusability of templates. With that freedom, you need to provide the checks to make sure that T
is of a reasonable type for the function.
Upvotes: 0
Reputation: 1356
It is the responsibility of the programmer to ensure that the data type being used for T
is compatible and has all the necessary operations that will performed on T
defined. As far as the C++ standard is concerned any data type can be used in place of T
there.
Upvotes: 1