Why can't static const float class members be inititalized in the class?

Discussion in 'C++' started by Martin Rennix, Apr 17, 2007.

  1. Eg

    class Foo
    {
    static const float f = 0.0f; // error
    static const int i = 0; // ok
    };

    What's the reasoning behind this seemingly arbitrary limitation?

    Martin
    Martin Rennix, Apr 17, 2007
    #1
    1. Advertising

  2. Martin Rennix wrote:
    > class Foo
    > {
    > static const float f = 0.0f; // error
    > static const int i = 0; // ok
    > };
    >
    > What's the reasoning behind this seemingly arbitrary limitation?


    I would guess portability, but don't quote me on that.

    Ask in comp.std.c++ -- they hold/discuss the reasoning behind any
    decisions that made it to the Standard.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 17, 2007
    #2
    1. Advertising

  3. Martin Rennix

    Ron Natalie Guest

    Re: Why can't static const float class members be inititalized inthe class?

    Martin Rennix wrote:
    > Eg
    >
    > class Foo
    > {
    > static const float f = 0.0f; // error
    > static const int i = 0; // ok
    > };
    >
    > What's the reasoning behind this seemingly arbitrary limitation?
    >
    > Martin
    >

    The compiler is only required to know how to do integer math at compile
    time.
    Ron Natalie, Apr 18, 2007
    #3
  4. On Apr 18, 8:41 am, Ron Natalie <> wrote:
    > MartinRennixwrote:
    > > Eg

    >
    > > class Foo
    > > {
    > > static const float f = 0.0f; // error
    > > static const int i = 0; // ok
    > > };

    >
    > > What's the reasoning behind this seemingly arbitrary limitation?

    >
    > > Martin

    >
    > The compiler is only required to know how to do integer math at compile
    > time.


    Is that because in the "olden days" you couldn't guarantee that a FPU
    would be available, or the equivalent software floating point library?
    Martin Rennix, Apr 18, 2007
    #4
  5. Martin Rennix

    James Kanze Guest

    On Apr 18, 5:20 am, Martin Rennix <> wrote:
    > On Apr 18, 8:41 am, Ron Natalie <> wrote:


    > > MartinRennixwrote:
    > > > Eg


    > > > class Foo
    > > > {
    > > > static const float f = 0.0f; // error
    > > > static const int i = 0; // ok
    > > > };


    > > > What's the reasoning behind this seemingly arbitrary limitation?


    > > The compiler is only required to know how to do integer math at compile
    > > time.


    Also, the fact that there is no need for it. It's really a
    hack, which was added for integers is because certain uses
    require an integral constant expression.

    > Is that because in the "olden days" you couldn't guarantee that a FPU
    > would be available, or the equivalent software floating point library?


    It's because even today, cross-compilers exist, and floating
    point results depend on the actual floating point being used.
    Something like 2. * 3.14 will have different values on different
    machines. And the standard doesn't impose emulation of the
    target floating point hardware.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 18, 2007
    #5
  6. Martin Rennix

    Ron Natalie Guest

    Re: Why can't static const float class members be inititalized inthe class?

    Martin Rennix wrote:

    >
    > Is that because in the "olden days" you couldn't guarantee that a FPU
    > would be available, or the equivalent software floating point library?
    >

    I think it was primarily there for crosscompilers. Integer math is
    pretty easy to do universally, floating point is a bit more involved.
    Not everybody uses the IEEE fp math.
    Ron Natalie, Apr 18, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JFCM
    Replies:
    4
    Views:
    5,719
  2. Rakesh Sinha
    Replies:
    4
    Views:
    1,838
    Rakesh Sinha
    Jan 13, 2005
  3. bd
    Replies:
    0
    Views:
    602
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,739
    Smokey Grindel
    Dec 2, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,502
    James Kanze
    Oct 8, 2009
Loading...

Share This Page