Static const non-integrals in-class initialization

Discussion in 'C++' started by Miguel Guedes, Aug 25, 2007.

  1. Hello,

    Why can't non-integral static const data members be initialized within a class?

    I know how to initialize the members but I don't understand the underlying
    reason to this rule. Would someone explain this to me?

    Example:

    class foo
    {
    public:
    static const float f = 0.02f; // Error!
    static const int i = 1; // Valid.
    };


    -- Miguel Guedes - X marks the spot for spammers. If you wish to get in touch
    with me by email, remove the X from my address. -
     
    Miguel Guedes, Aug 25, 2007
    #1
    1. Advertising

  2. Miguel Guedes

    red floyd Guest

    Miguel Guedes wrote:
    > Hello,
    >
    > Why can't non-integral static const data members be initialized within a class?
    >
    > I know how to initialize the members but I don't understand the underlying
    > reason to this rule. Would someone explain this to me?
    >
    > Example:
    >
    > class foo
    > {
    > public:
    > static const float f = 0.02f; // Error!
    > static const int i = 1; // Valid.
    > };
    >
    >


    Because the Standard says so. I don't know if this has been changed in
    the draft 0x standard.
     
    red floyd, Aug 25, 2007
    #2
    1. Advertising

  3. red floyd wrote:
    > Miguel Guedes wrote:
    >> Hello,
    >>
    >> Why can't non-integral static const data members be initialized within a class?
    >>
    >> I know how to initialize the members but I don't understand the underlying
    >> reason to this rule. Would someone explain this to me?
    >>
    >> Example:
    >>
    >> class foo
    >> {
    >> public:
    >> static const float f = 0.02f; // Error!
    >> static const int i = 1; // Valid.
    >> };
    >>
    >>

    >
    > Because the Standard says so. I don't know if this has been changed in
    > the draft 0x standard.
    >


    Well, yes. But I'm interested in knowing the reason why they chose to
    differentiate the way non-integrals are initialized. Anyone knows?


    --
    Miguel Guedes

    - X marks the spot for spammers. If you wish to get in touch with me by email,
    remove the X from my address. -
     
    Miguel Guedes, Aug 25, 2007
    #3
  4. Miguel Guedes

    Ondra Holub Guest

    On 25 Srp, 13:30, Miguel Guedes <> wrote:
    > red floyd wrote:
    > > Miguel Guedes wrote:
    > >> Hello,

    >
    > >> Why can't non-integral static const data members be initialized within a class?

    >
    > >> I know how to initialize the members but I don't understand the underlying
    > >> reason to this rule. Would someone explain this to me?

    >
    > >> Example:

    >
    > >> class foo
    > >> {
    > >> public:
    > >> static const float f = 0.02f; // Error!
    > >> static const int i = 1; // Valid.
    > >> };

    >
    > > Because the Standard says so. I don't know if this has been changed in
    > > the draft 0x standard.

    >
    > Well, yes. But I'm interested in knowing the reason why they chose to
    > differentiate the way non-integrals are initialized. Anyone knows?
    >
    > --
    > Miguel Guedes
    >
    > - X marks the spot for spammers. If you wish to get in touch with me by email,
    > remove the X from my address. -


    I am only guessing. Maybe this way may be initialized only these
    types, which are allowed as template parameters. But I do not know why.
     
    Ondra Holub, Aug 25, 2007
    #4
  5. Miguel Guedes

    BobR Guest

    Miguel Guedes <> wrote in message...
    > red floyd wrote:
    > > Miguel Guedes wrote:
    > >> Why can't non-integral static const data members be initialized within

    a class?
    > >> I know how to initialize the members but I don't understand the

    underlying
    > >> reason to this rule. Would someone explain this to me?

    > >
    > > Because the Standard says so. I don't know if this has been changed in
    > > the draft 0x standard.

    >
    > Well, yes. But I'm interested in knowing the reason why they chose to
    > differentiate the way non-integrals are initialized. Anyone knows?


    Then you should ask in an NG that deals with those issues. Maybe
    comp.std.c++.

    Until *all* systems agree on the float/double format, you probably won't see
    it happen.
    Take a trip through <limits> to see what has to be dealt with.
    Is it FPU or is it emulated? (memorex ad)<G>

    --
    Bob R
    POVrookie
     
    BobR, Aug 25, 2007
    #5
  6. * Miguel Guedes:
    > red floyd wrote:
    >> Miguel Guedes wrote:
    >>> Hello,
    >>>
    >>> Why can't non-integral static const data members be initialized within a class?
    >>>
    >>> I know how to initialize the members but I don't understand the underlying
    >>> reason to this rule. Would someone explain this to me?
    >>>
    >>> Example:
    >>>
    >>> class foo
    >>> {
    >>> public:
    >>> static const float f = 0.02f; // Error!
    >>> static const int i = 1; // Valid.
    >>> };
    >>>
    >>>

    >> Because the Standard says so. I don't know if this has been changed in
    >> the draft 0x standard.
    >>

    >
    > Well, yes. But I'm interested in knowing the reason why they chose to
    > differentiate the way non-integrals are initialized. Anyone knows?


    No. It has been discussed before, several times. And the upshot seems
    to be that if other types than integral types were to be allowed, then
    it was feared that this would open up a hornet's nest of issues the
    commitee would then have to deal with. I'm not sure what those issues
    would be; constants of class types have been mentioned, but I think
    that's a red herring class misunderstanding.

    But note that the current standard only allows use of such constants as
    pure compile time values (i.e. not taking address), unless you define
    the constant outside the class, i.e. in a separately compiled file, e.g. as

    int foo::i;

    which looks suspiciously like a pure declaration, but is actually a
    definition, and what looks like a definition in the class definition, is
    actually a pure declaration. Formally. And different compilers differ
    in how strictly they interpret the standard, or how they interpret it.

    In other words, the whole thing is a kludge, probably tacked on to the
    language because of pressure from some sub-group of the committee, and
    what was accepted was probably the minimum needed to make 'em shut up.

    :)

    Now, as device for introducing a named, typed non-integral constant in
    the class definition /in a header file/, without any separately compiled
    definition, you can alternatively use the templated constant trick,
    which as far as I know has only been put forward by me, and not even I
    use it -- it's strictly a "yes there is a (painful) way" thing:

    #include <iostream>
    #include <ostream>

    template< typename Dummy > struct FConstant { static float const f; };
    template< typename Dummy > float const FConstant<Dummy>::f = 0.02f;

    class Foo
    : public FConstant<void>
    {
    // Whatever, here we have foo::f.
    };

    int main()
    {
    std::cout << Foo::f << std::endl;
    }

    Checking that this compiles & works... Yes. So a standard-conforming
    C++ compiler has no problem introducing such a constant, it is
    /required/ to support this, provided it's supplied with the proper magic
    incantation, and as mentioned above, why the committee didn't allow a
    more natural and concise way to write that, well, politics, probably...

    Enjoy (if u like pain!).

    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 25, 2007
    #6
  7. Miguel Guedes

    Ron Natalie Guest

    red floyd wrote:
    > Miguel Guedes wrote:
    >> Hello,
    >>
    >> Why can't non-integral static const data members be initialized within a class?
    >>
    >> I know how to initialize the members but I don't understand the underlying
    >> reason to this rule. Would someone explain this to me?
    >>
    >> Example:
    >>
    >> class foo
    >> {
    >> public:
    >> static const float f = 0.02f; // Error!
    >> static const int i = 1; // Valid.
    >> };
    >>
    >>

    >
    > Because the Standard says so. I don't know if this has been changed in
    > the draft 0x standard.
    >

    The issue is that the compiler is expectedto know how to execute
    const integral expressions at compile time, so the const inline int
    makes sense. The compiler is not required to be able to do floating
    point at compile time (it may assume that it's too hard to crosscompile).
     
    Ron Natalie, Aug 25, 2007
    #7
  8. * Ron Natalie:
    > red floyd wrote:
    >> Miguel Guedes wrote:
    >>> Hello,
    >>>
    >>> Why can't non-integral static const data members be initialized
    >>> within a class?
    >>>
    >>> I know how to initialize the members but I don't understand the
    >>> underlying
    >>> reason to this rule. Would someone explain this to me?
    >>>
    >>> Example:
    >>>
    >>> class foo
    >>> {
    >>> public:
    >>> static const float f = 0.02f; // Error!
    >>> static const int i = 1; // Valid.
    >>> };
    >>>
    >>>

    >>
    >> Because the Standard says so. I don't know if this has been changed in
    >> the draft 0x standard.
    >>

    > The issue is that the compiler is expectedto know how to execute
    > const integral expressions at compile time, so the const inline int
    > makes sense. The compiler is not required to be able to do floating
    > point at compile time (it may assume that it's too hard to crosscompile).


    Sorry, while that is relevant it's not a valid reason to forbid the
    syntax. It's only a valid reason to forbid the use of e.g. a named
    double constant at compile time (e.g. casted to int). As I've shown
    else-thread, you can easily achieve the effect of having a constant
    float foo::f, so any standard-conforming compiler must support the
    effective functionality, just not the sensible syntax: it's explicitly
    not allowed to support the sensible /syntax/, so the question is why on
    Earth not, and the answer, probably, politics and arbitrary history.

    Cheers,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 25, 2007
    #8
    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. Rakesh Sinha
    Replies:
    4
    Views:
    1,878
    Rakesh Sinha
    Jan 13, 2005
  2. Dave
    Replies:
    10
    Views:
    35,400
    Ron Natalie
    May 22, 2005
  3. Javier
    Replies:
    2
    Views:
    622
    James Kanze
    Sep 4, 2007
  4. er
    Replies:
    3
    Views:
    402
  5. fungus
    Replies:
    13
    Views:
    948
    fungus
    Oct 31, 2008
Loading...

Share This Page