tags:

views:

88

answers:

1
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
+3  A: 

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.

James McNellis
I don't have the draft on me, does C++0x allow any numeric type?
GMan
@GMan: I remember it was proposed that any numeric type be allowed, but N3092 §14.3.2/1 still says "an integral constant expression," so it appears not.
James McNellis
I don't know why this rule is a good idea.
Neil G
@Neil: Well, you could specify the lower bound as an integer, if that's a viable option. Otherwise, your best bet might be not to make the lower bound part of the type (i.e., pass the lower bound into the constructor when you construct the type).
James McNellis
@James, yeah I remember hearing it too, too bad it didn't happen. Thanks for checking.
GMan
@Neil: It's not a good rule. There's no real technical reason for the restriction, it's just the way the language is specified.
James McNellis
@James McNellis: Okay, thanks for your quick answer. Much appreciated.
Neil G
Well, it would require standardizing precision and rounding w.r.t. floating point arithmetic, wouldn't it? IMHO, that's a no go.
sellibitze
Well, you can make it take a `int` but specify the value using a user defined literal like `0.0_myfloat`. This literal could call a literal operator template, which returns a literal class type with a constexpr conversion function to `int`, returning a fixed-point decimal.
Johannes Schaub - litb
@sellibitze: Well, such details are implementation dependent, yes, but such details are (at least typically) consistent within an implementation, so `T<0.0>` would always yield the same instantiation. There was at least one proposal to allow any literal to be used as a nontype parameter but it was rejected (without comment, it seems; either that or I just can't find the comments related to that).
James McNellis