Neil G
Neil G

Reputation: 33202

template fails to compile: 'double' is not a valid type for a template constant parameter

template<typename T, T Min>
class LowerBoundedType {};
template<typename T> class vectorelement {};
template<> class vectorelement<Categorical> { typedef LowerBoundedType<double, 0.0> type; };

with error:

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

Upvotes: 11

Views: 4749

Answers (2)

angelos.p
angelos.p

Reputation: 473

A nontype template parameter can only one of the followings:

  • integral type (e.g. int or long;)
  • enumeration type
  • a pointer or a reference to an object
  • a pointer or a reference to a function
  • a pointer to a member of a class.

Therefore it cannot be a double type or a class type. In general, the primary rationale for nontype parameters is to allow sizes and range limits for containers.

Upvotes: 0

James McNellis
James McNellis

Reputation: 355009

The only numeric types valid for a nontype template parameter are integers and enumerations. So, you can't have a nontype template parameter of type double.

Upvotes: 12

Related Questions