Why template type parameters cannot be class type

class Example { // ... }; template <typename T, Example ex> //Error class MyExample{ // ... }; 

My question is why non-type patterns cannot be class types?

The error I get is

error: 'class Example' is not a valid type for a template constant parameter

+6
c ++ types templates
source share
2 answers

Just because it's the rules. Rationally, template parameters should be allowed at compile time, and objects of the class type are only created (even temporary and those with static storage time) at run time. You can only have template parameters that are "values" allowed at compile time, such as integers and types. However, it is possible to have template parameters that are pointers or object references.

+13
source share

According to C ++ standard ,

 A non-type template-parameter shall have one of the following (optionally cv-qualified) types: — integral or enumeration type, — pointer to object or pointer to function, — reference to object or reference to function, — pointer to member. A non-type template-parameter shall not be declared to have floating point, **class**, or void type. 

Obviously, any std-compatible compiler throws an error if you declare a non-template argument.

+3
source share

All Articles