Topic: Doubles and Floats as Template Parameters


Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/05/09
Raw View
Jim McKelvey wrote:
>
> According to the latest public draft standard (see 14.1(3)), non-type
> template-parameters may not be of floating type.
>
> Why this restriction? It doesn't show up in the ARM, nor in _Design
> and Evolution of C++_.

I think it's to avoid requiring that the compiler be able to duplicate
precisely the floating point arithmetic of the target machine. To do so
might be difficult in a cross-compiler. For instance, two float
constants that are almost identical using the floating point hardware on
the target machine might turn out to be identical in the floating point
hardware on the machine doing the compilation, or vice versa. To make
the compiler simulate the floating point hardware on the target machine
is probably too costly to be worthwhile, whereas the same isn't true for
integers.

--

Ciao,
Paul


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Jim McKelvey <mckelvey@bean.jpl.nasa.gov>
Date: 1998/05/04
Raw View
According to the latest public draft standard (see 14.1(3)), non-type
template-parameters may not be of floating type.

Why this restriction? It doesn't show up in the ARM, nor in _Design and
Evolution of C++_.

It's especially odd since floating types as type-parameters are allowed,
as well as pointers or references to floating types.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/05/05
Raw View
In article 3802@bean.jpl.nasa.gov, Jim McKelvey <mckelvey@bean.jpl.nasa.gov> writes:
>According to the latest public draft standard (see 14.1(3)), non-type
>template-parameters may not be of floating type.
>
>Why this restriction? It doesn't show up in the ARM, nor in _Design and
>Evolution of C++_.
>
>It's especially odd since floating types as type-parameters are allowed,
>as well as pointers or references to floating types.

The ARM originally didn't allow non-type template parameters. It's
new with the C++ draft standard, and at no time were floating-points
allowed as non-type template parameters.

It's because of the lack of definiteness in the value of a floating-
point expression. Consider: If we have
 template < int i > class T { ... };
then each mention of T with the same value for i has the same type,
and any mention of T with a different value for i has a different type:
 T<3>        // a type
 T<2+1>      // same type
 T<10/(2+1)> // same type
 T<2>        // different type

This all works because the results of integer arithmetic are well-
specified. Not so with floating-point. Example:
 template < double x > class T { ... };
 T<3.0>      // a type
 T<0.3/0.1>  // ???
 T<2.9999999999999999909 +
   0.0000000000000000091> // ???

Whether any two of these are the same type depends on the
characteristics of floating-point arithmetic in the compiler.
Since you can't know in general whether two types that appear
to be the same really will be the same, floating-point constants
don't have the same status as integer constants. (For example,
you can't use floating-point arithmetic anywhere in an integer-
constant-expression.)

You could argue that you can write an integer expression whose value
depends on the implementation, but that situation occurs for well-
defined edge cases. No sensible programmer will use a "short"
parameter if values that require more than 16 bits are to be allowed,
for example. The problem is fundamental to the way floating-point
is defined (or hardly defined at all) in C++.


---
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jkanze@otelo.ibmmail.com
Date: 1998/05/05
Raw View
In article <6iladp$n2h@engnews1.Eng.Sun.COM>#1/1,
  clamage@Eng.Sun.COM wrote:

> The ARM originally didn't allow non-type template parameters. It's
> new with the C++ draft standard, and at no time were floating-points
> allowed as non-type template parameters.

Are you sure?  I don't have a copy of the ARM handy to verify, but from
memory, non-type template parameters were always supported for class
templates.  (The ARM didn't allow non-type parameters for function
templates.)

All compilers I've used which supported templates, including CFront
3.0 (the first compiler with template support) have supported non-type
templates.  (With regards to the original question, none have supported
floating point parameters.)

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
        +49 (0)69 66 45 33 10    mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient   e objet --
              -- Beratung in objektorientierter Datenverarbeitung

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jbuck@best.com (Joe Buck)
Date: 1998/05/06
Raw View
Steve Clamage <clamage@Eng.Sun.COM> wrote:
>The ARM originally didn't allow non-type template parameters. It's
>new with the C++ draft standard, and at no time were floating-points
>allowed as non-type template parameters.

Steve, your memory is off.  I checked my copy of the ARM, and sure enough,
they are in there.  Section 14.2c discusses them.  The only restriction
mentioned is "Nontype template arguments are restricted to values that
can be compared at compile time.  This allows static type checking involving
the resulting types."

Given this, yes, forbidding floating point is a new restriction, though
there are good reasons for the restriction as you point out.

(My copy of the ARM is the original 1990 version).

--
-- Joe Buck
   work: jbuck@synopsys.com, otherwise jbuck@welsh-buck.org or jbuck@best.net
http://www.welsh-buck.org/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jkanze@otelo.ibmmail.com
Date: 1998/05/06
Raw View
In article <6io4ej$frk$1@shell17.ba.best.com>#1/1,
  jbuck@best.com (Joe Buck) wrote:
>
> Steve Clamage <clamage@Eng.Sun.COM> wrote:
> >The ARM originally didn't allow non-type template parameters. It's
> >new with the C++ draft standard, and at no time were floating-points
> >allowed as non-type template parameters.
>
> Steve, your memory is off.  I checked my copy of the ARM, and sure enou=
gh,
> they are in there.  Section 14.2c discusses them.  The only restriction
> mentioned is "Nontype template arguments are restricted to values that
> can be compared at compile time.  This allows static type checking
involving
> the resulting types."
>
> Given this, yes, forbidding floating point is a new restriction, though
> there are good reasons for the restriction as you point out.

Given this restriction, forbidding floating point is NOT a new
restriction, at least supposing that the C rules hold for things not
rigorously specified in the ARM.  C does not require compile time
floating point arithmetic, nor compile time floating point comparisons.
Thus, floating point values cannot be compared at compile time.

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
        +49 (0)69 66 45 33 10    mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient=E9e objet --
              -- Beratung in objektorientierter Datenverarbeitung

-----=3D=3D Posted via Deja News, The Leader in Internet Discussion =3D=3D=
-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]