Discussion:
floating point intermediate result in template parameter
(too old to reply)
Mycroft Holmes
2009-12-09 08:15:25 UTC
Permalink
Hi,

I was pretty surprised when VC2008 happily compiled the code below
(even with language extensions disabled): I noticed that Comeau
rejects it in strict mode, but accepts in relaxed mode.
I was convinced it's illegal to have floating-point intermediate
results when computing a static constant IIRC, the motivation was that
floating point operations, such as rounding, may depend on a state of
the processor which is available only at runtime.
Do I remember correctly? Is there any plan to change it in the
standard?


template <int N>
struct test
{
static const int value = int(N * 1.2);
};

int main()
{
return test<7>::value;
}
James Kanze
2009-12-09 09:14:44 UTC
Permalink
Post by Mycroft Holmes
I was pretty surprised when VC2008 happily compiled the code
below (even with language extensions disabled): I noticed that
Comeau rejects it in strict mode, but accepts in relaxed mode.
I was convinced it's illegal to have floating-point
intermediate results when computing a static constant IIRC,
the motivation was that floating point operations, such as
rounding, may depend on a state of the processor which is
available only at runtime. Do I remember correctly? Is there
any plan to change it in the standard?
template <int N>
struct test
{
static const int value = int(N * 1.2);
};
int main()
{
return test<7>::value;
}
According to the current standard, an integral constant
expression is required in this context. In an integral constant
expression, "Floating literals can appear only if they are cast
to integral or enumerated types". In other words, N *
(int)(1.2) is legal, but (int)(N * 1.2) isn't.

The whole concept of "constant expression" has been reworked in
the next version of the standard, in order to allow things like
std::numeric_limits<int>::max() to be used in a constant
expression (even though it is a function). At least in the more
or less recent version of the draft I have on hand (N2914), it
looks like the restriction will be lifted, and that the above
expression will be legal. I suspect, however, that the exact
results of floating point arithmetic are not guaranteed to be
the same at compile time and at run time. (In fact, the exact
results of floating point arithmetic aren't guaranteed to be the
same for two different evaluations at run-time.)

--
James Kanze
Mycroft Holmes
2009-12-09 10:35:50 UTC
Permalink
Post by James Kanze
At least in the more
or less recent version of the draft I have on hand (N2914), it
looks like the restriction will be lifted, and that the above
expression will be legal.  
thanks for the detailed reply.
do you also know if the restriction that template parameters cannot be
floating-point will be removed?
Igor Tandetnik
2009-12-09 12:38:22 UTC
Permalink
Post by Mycroft Holmes
do you also know if the restriction that template parameters cannot be
floating-point will be removed?
No. 14.2p7: A non-type template-parameter shall not be declared to have floating point, class, or void type.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Loading...