Topic: Floating Value Template Arguments
Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/05/28 Raw View
In article <3pi708$ihm@offas_dike.sbil.co.uk>,
Marc Shepherd <shepherd@debussy.sbi.com> wrote:
>Clause 14 of the draft WP (the "templates" clause) says:
>
> A non-type template-parameter shall not be of floating type.
> [Example:
>
> template<double d> class X; // error
> template<double* pd> class X; // ok
> template<double& rd> class X; // ok
>
> --end example]
>
>Although no rationale is given, one assumes the restriction
>was to prevent a proliferation of instantiations for floating
>values that differ only by 'epsilon'.
Partly. The real explanation is that non-type
parameters bind to _constant values_: there are no
floating point constant expressions.
>If that was the rationale, then I fail to see why a double
>reference is allowed as a template parameter, since a double
>reference can refer to the same value range as a double can.
You miss the point. The value is irrelevant.
A reference in this context denotes an address.
>If there was a different rationale for prohibiting floating
>values as template parameters, could someone on the committee
>explain what it was?
Consider:
template<double &> class X { ..}
const double d = 1.0;
X<d> x; // ERROR
This is an error AS IS
template<double *> class X { ..}
const double d = 1.0;
X<&d> x; // ERROR
and for the same reason. The argument which is a pointer
or reference MUST BE A NAMED VARIABLE WITH EXTERNAL LINKAGE
plus or minus a constant expression.
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/05/22 Raw View
shepherd@debussy.sbi.com (Marc Shepherd) writes:
> A non-type template-parameter shall not be of floating type.
>
>Although no rationale is given [...]
The ARM states:
"Nontype template arguments are restricted to values that
can be compared at compile time. This allows static type
checking involving the resulting types."
If floating point template arguments were allowed, the equivalence of
types such as X<1.0> and X<3*0.333333333> would be implementation
dependent, which would be undesireable for a variety of reasons, one of
which is that cross-compilers might be required to emulate the target's
floating point capabilities.
>[...] I fail to see why a double
>reference is allowed as a template parameter, since a double
>reference can refer to the same value range as a double can.
Allowing references to floats does not cause the above problem
of implementation-dependent type equivalence.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
Author: shepherd@debussy.sbi.com (Marc Shepherd)
Date: 1995/05/19 Raw View
Clause 14 of the draft WP (the "templates" clause) says:
A non-type template-parameter shall not be of floating type.
[Example:
template<double d> class X; // error
template<double* pd> class X; // ok
template<double& rd> class X; // ok
--end example]
Although no rationale is given, one assumes the restriction
was to prevent a proliferation of instantiations for floating
values that differ only by 'epsilon'. (In other words,
floating-valued template parameters were banned for the
same reason that well-written programs should never compare
two floating values for *exact* equality.)
If that was the rationale, then I fail to see why a double
reference is allowed as a template parameter, since a double
reference can refer to the same value range as a double can.
If there was a different rationale for prohibiting floating
values as template parameters, could someone on the committee
explain what it was?
---
Marc Shepherd
Salomon Brothers Inc
shepherd@schubert.sbi.com The opinions I express are no one's but mine!
Author: jhs@edg.com (John H. Spicer)
Date: 1995/05/19 Raw View
In article <3pi708$ihm@offas_dike.sbil.co.uk> shepherd@debussy.sbi.com writes:
>Clause 14 of the draft WP (the "templates" clause) says:
>
> A non-type template-parameter shall not be of floating type.
> [Example:
>
> template<double d> class X; // error
> template<double* pd> class X; // ok
> template<double& rd> class X; // ok
>
> --end example]
>
>Although no rationale is given, one assumes the restriction
>was to prevent a proliferation of instantiations for floating
>values that differ only by 'epsilon'. (In other words,
>floating-valued template parameters were banned for the
>same reason that well-written programs should never compare
>two floating values for *exact* equality.)
>
>If that was the rationale, then I fail to see why a double
>reference is allowed as a template parameter, since a double
>reference can refer to the same value range as a double can.
A double reference does not refer to a value, but to an object
that is required to have external linkage. So if you say
template <double& rd> struct A {};
double d = 1.0;
A<d> ad;
You are not instantiating A with the value 1.0, you are instantiating
A with a reference to the variable d. The value of d can change while
the program is running.
>
>If there was a different rationale for prohibiting floating
>values as template parameters, could someone on the committee
>explain what it was?
The rationale you gave is basically the reason this was prohibited
by the committee. For example:
template <double d> struct A {};
A<.2> a1;
A<1.0/5.0> a2; // Same type as a1?
In cases like this it is impossible for the user to tell if two types
will be the same or not. It may also vary from one system to another.
John Spicer
Edison Design Group