g++ thingy

Discussion in 'C++' started by keith, Feb 14, 2010.

  1. keith

    keith Guest

    Why does g++ 4.3.4 accept this struct declaration?

    struct A
    {
    static float const c = .5;
    };

    This compiles ok, I can reference the constant without any problems. The
    linker gives no errors.
     
    keith, Feb 14, 2010
    #1
    1. Advertising

  2. keith

    Robert Fendt Guest

    And thus spake keith <>
    Sun, 14 Feb 2010 17:16:36 +0100:

    > Why does g++ 4.3.4 accept this struct declaration?


    Hint: retry with -pedantic -ansi -Werror.

    Your question is essentially the same as "Why do compiler
    manufacturers implement non-standard extenstions?"

    Answer: mainly because they can, and because some extensions
    _are_ useful, if one is not interested in portability.

    Regards,
    Robert
     
    Robert Fendt, Feb 14, 2010
    #2
    1. Advertising

  3. keith wrote:
    > Why does g++ 4.3.4 accept this struct declaration?
    >
    > struct A
    > {
    > static float const c = .5;
    > };


    Why shouldn't it?


    Marcel
     
    Marcel Müller, Feb 14, 2010
    #3
  4. keith

    Rolf Magnus Guest

    Marcel Müller wrote:

    > keith wrote:
    >> Why does g++ 4.3.4 accept this struct declaration?
    >>
    >> struct A
    >> {
    >> static float const c = .5;
    >> };

    >
    > Why shouldn't it?


    Because the C++ standard doesn't allow it.
     
    Rolf Magnus, Feb 14, 2010
    #4
  5. keith

    keith Guest

    > Hint: retry with -pedantic -ansi -Werror.

    Thanks.

    > Your question is essentially the same as "Why do compiler
    > manufacturers implement non-standard extenstions?"
    >
    > Answer: mainly because they can, and because some extensions
    > _are_ useful, if one is not interested in portability.


    I don't agree completely, as gcc is present on many architectures, or
    rather, as gcc is itself very portable, portability does not suffer much
    if one uses its extensions.

    Maybe this g++ extension is documented somewhere?
     
    keith, Feb 14, 2010
    #5
  6. keith

    keith Guest

    >> Why does g++ 4.3.4 accept this struct declaration?
    >>
    >> struct A
    >> {
    >> static float const c = .5;
    >> };

    >
    > Why shouldn't it?


    it's non-standard.
     
    keith, Feb 14, 2010
    #6
  7. Marcel Müller wrote, On 14.2.2010 18:24:
    > keith wrote:
    >> Why does g++ 4.3.4 accept this struct declaration?
    >>
    >> struct A { static float const c = .5; };

    >
    > Why shouldn't it?

    Because IIRC only integral types constants can be initialized at the
    declaration like that.


    --
    VH
     
    Vaclav Haisman, Feb 14, 2010
    #7
  8. On 2010-02-14, keith <> wrote:
    >> Hint: retry with -pedantic -ansi -Werror.

    >
    > Thanks.
    >
    >> Your question is essentially the same as "Why do compiler
    >> manufacturers implement non-standard extenstions?"
    >>
    >> Answer: mainly because they can, and because some extensions
    >> _are_ useful, if one is not interested in portability.

    >
    > I don't agree completely, as gcc is present on many architectures, or
    > rather, as gcc is itself very portable, portability does not suffer much
    > if one uses its extensions.
    >


    Nonetheless, C++ is far more portable than "GNU C++" and
    also far less likely to change without warning.

    > Maybe this g++ extension is documented somewhere?
     
    Andrew Poelstra, Feb 14, 2010
    #8
  9. keith

    Krice Guest

    On 14 helmi, 19:46, keith <> wrote:
    > portability does not suffer much if one uses its extensions.


    Actually it suffers a lot from non-standard extensions
    used by ignorant simpletons. But you don't seem to be a
    kind of guy who knows something about portability.
     
    Krice, Feb 14, 2010
    #9
  10. keith

    keith Guest

    Krice pravi:
    > On 14 helmi, 19:46, keith <> wrote:
    >> portability does not suffer much if one uses its extensions.

    >
    > Actually it suffers a lot from non-standard extensions
    > used by ignorant simpletons. But you don't seem to be a
    > kind of guy who knows something about portability.


    All unsubstantiated assertions, especially since they come from a Java
    programmer.
     
    keith, Feb 14, 2010
    #10
  11. keith

    Rolf Magnus Guest

    Pete Becker wrote:

    > Rolf Magnus wrote:
    >> Marcel Müller wrote:
    >>
    >>> keith wrote:
    >>>> Why does g++ 4.3.4 accept this struct declaration?
    >>>>
    >>>> struct A
    >>>> {
    >>>> static float const c = .5;
    >>>> };
    >>> Why shouldn't it?

    >>
    >> Because the C++ standard doesn't allow it.
    >>

    >
    > Sure it does. The construct make the program ill-formed, and a
    > conforming implementation is required to issue a diagnostic. Having done
    > that, the implementation is free to do pretty much anything, including
    > "accept" this struct declaration.


    Depends on how you define "accept", but I get your point.
     
    Rolf Magnus, Feb 14, 2010
    #11
  12. keith

    Helge Kruse Guest

    "keith" <> wrote in message
    news:hl9csv$oak$-september.org...
    >> Answer: mainly because they can, and because some extensions
    >> _are_ useful, if one is not interested in portability.

    >
    > I don't agree completely, as gcc is present on many architectures, or
    > rather, as gcc is itself very portable, portability does not suffer much
    > if one uses its extensions.


    But the code is not portable. Portability is not only a question of
    different architectures. You need to port code from one compiler to another.

    Helge
     
    Helge Kruse, Feb 15, 2010
    #12
  13. keith

    Jorgen Grahn Guest

    On Sun, 2010-02-14, keith wrote:
    >> Hint: retry with -pedantic -ansi -Werror.

    >
    > Thanks.


    Not to mention -Wall -Wextra.

    >> Your question is essentially the same as "Why do compiler
    >> manufacturers implement non-standard extenstions?"
    >>
    >> Answer: mainly because they can, and because some extensions
    >> _are_ useful, if one is not interested in portability.

    >
    > I don't agree completely, as gcc is present on many architectures, or
    > rather, as gcc is itself very portable, portability does not suffer much
    > if one uses its extensions.


    - You do not always (read: almost never) have the option to choose the
    compiler to use.
    - Most programmers will be unfamiliar with the extension.
    - You'll probably have to disable all kinds of useful warnings
    in order to enable the extension.

    > Maybe this g++ extension is documented somewhere?


    You're that interested in it, and yet you haven't read about it in the
    manual?

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 15, 2010
    #13
  14. keith

    keith Guest

    > You're that interested in it, and yet you haven't read about it in the
    > manual?


    I did not notice it in the manual, maybe it is undocumented? I stumbled
    on it by chance.
     
    keith, Feb 20, 2010
    #14
  15. keith

    Jorgen Grahn Guest

    On Sat, 2010-02-20, keith wrote:
    >> You're that interested in it, and yet you haven't read about it in the
    >> manual?

    >
    > I did not notice it in the manual, maybe it is undocumented? I stumbled
    > on it by chance.


    You have a point -- I don't know exactly where to start and it's hard
    to search for. Perhaps it gets easier when you see the error message
    (which I had to add -pedantic to get):

    tuva:/tmp> g++ -Wall -Wextra -std=c++98 -pedantic -c f.cc
    f.cc:3: error: floating-point literal cannot appear in a
    constant-expression
    f.cc:3: error: ISO C++ forbids initialization of member constant 'c'
    of non-integral type 'const float'

    I'm not really interested in the feature, but I *am* interested in
    making more people enable the warnings in their compilers. It saves
    so much trouble if you do it from day one.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 28, 2010
    #15
    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. M.E.
    Replies:
    0
    Views:
    352
  2. David Graham

    bucies image selector thingy

    David Graham, Jul 3, 2003, in forum: HTML
    Replies:
    4
    Views:
    555
    brucie
    Jul 3, 2003
  3. Robbie

    loading message thingy

    Robbie, Aug 11, 2004, in forum: Python
    Replies:
    3
    Views:
    267
    John J. Lee
    Aug 12, 2004
  4. Gernot Frisch

    how to implement a "typoef" thingy.

    Gernot Frisch, Jan 16, 2006, in forum: C++
    Replies:
    8
    Views:
    300
    Gernot Frisch
    Jan 16, 2006
  5. Mark Hobley

    Another doubleunderscore thingy

    Mark Hobley, Feb 28, 2010, in forum: C Programming
    Replies:
    11
    Views:
    897
    Ersek, Laszlo
    Mar 1, 2010
Loading...

Share This Page