Re: numeric_limits and constexpr
Marc wrote:
Hello,
I notice that the default numeric_limits in C++0X reads:
static constexpr T min() throw() { return T(); }
I assume this fails if T doesn't have a constexpr default
constructor (T isn't a literal type).
If fails as a constant expression, but is still legal in places where
a constant isn't requiered (assuming T() is valid, but not constexpr).
It's never been clear whether users were supposed to provide a
specialization of numeric_limits for their own number types, but
this sounds like one has to be careful using numeric_limits.
If the class is to behave as a numeric type, it would probably need a
specialization. The default numeric_limits class only provides dummy
values for most of its members.
If I write something like:
if(numeric_limits<T>::is_bounded) {
... use numeric_limits<T>::min() ...
}
do I have the guarantee that the compiler won't try to instantiate
min for wrong types? (I could test std::is_builtin<T>::value (can't
remember the real name) but the issue is the same) Or do I need to
write 2 functions?
The compiler will have to compile all parts of the if-statement, even
those that are statically known never to execute. If you want to avoid
this, you will probably have to use some meta programming, like using
C++0x std::enable_if.
Since there is no has_epsilon, for a type that has
numeric_limits<T>::is_specialized, I would check epsilon()==T()
which is a bad idea. Should I assume that only float, double and
long double have epsilon?
Or any other type with a user specialized numeric_limits.
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]