weird warning with g++ 4.1

Discussion in 'C++' started by Sam Steingold, Sep 27, 2006.

  1. the following program does not compile with g++ 4.1.1:
    ====================================================
    // $Id$
    // $Source$

    #include <stdint.h>
    #include <stdio.h>

    typedef struct { uint32_t one_c; } chart;
    #define as_chart(c) ((chart){one_c:(c)})
    #define ascii(x) as_chart((uint8_t)(x))
    #define CR 13
    #define LF 10

    static void wr_ch_unbuffered_dos (void) {
    static chart const crlf[2] = { ascii(CR), ascii(LF) };
    for (int i = 0; i < 2; i++)
    printf("%d\n",crlf.one_c);
    }

    int main (int argc, char *argv[]) {
    printf("%d %s\n",argc,argv[0]);
    wr_ch_unbuffered_dos();
    return 0;
    }
    ====================================================
    cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
    cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
    cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
    cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
    cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization

    the bad line is
    static chart const crlf[2] = { ascii(CR), ascii(LF) };

    I am pretty sure code like this worked with g++ 3.
    What do I do now?
    Thanks!

    $ g++ --version
    g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)

    --
    Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
    http://palestinefacts.org http://camera.org http://truepeace.org
    http://israelunderattack.slide.com http://dhimmi.com http://pmw.org.il
    The paperless office will become a reality soon after the paperless toilet.
     
    Sam Steingold, Sep 27, 2006
    #1
    1. Advertising

  2. Sam Steingold

    mlimber Guest

    Sam Steingold wrote:
    > the following program does not compile with g++ 4.1.1:
    > ====================================================
    > // $Id$
    > // $Source$
    >
    > #include <stdint.h>
    > #include <stdio.h>
    >
    > typedef struct { uint32_t one_c; } chart;
    > #define as_chart(c) ((chart){one_c:(c)})
    > #define ascii(x) as_chart((uint8_t)(x))
    > #define CR 13
    > #define LF 10
    >
    > static void wr_ch_unbuffered_dos (void) {
    > static chart const crlf[2] = { ascii(CR), ascii(LF) };
    > for (int i = 0; i < 2; i++)
    > printf("%d\n",crlf.one_c);
    > }
    >
    > int main (int argc, char *argv[]) {
    > printf("%d %s\n",argc,argv[0]);
    > wr_ch_unbuffered_dos();
    > return 0;
    > }
    > ====================================================
    > cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
    > cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
    > cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
    > cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
    > cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
    >
    > the bad line is
    > static chart const crlf[2] = { ascii(CR), ascii(LF) };
    >
    > I am pretty sure code like this worked with g++ 3.
    > What do I do now?
    > Thanks!
    >
    > $ g++ --version
    > g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)


    Looks like you're trying to use C99 features that C++ does not support.
    Look for a compiler flag to enable those extensions, or change your
    code to valid C++ syntax.

    Cheers! --M
     
    mlimber, Sep 27, 2006
    #2
    1. Advertising

  3. > * mlimber <> [2006-09-27 07:40:58 -0700]:
    >
    > Sam Steingold wrote:
    >> the following program does not compile with g++ 4.1.1:
    >> ====================================================
    >> // $Id$
    >> // $Source$
    >>
    >> #include <stdint.h>
    >> #include <stdio.h>
    >>
    >> typedef struct { uint32_t one_c; } chart;
    >> #define as_chart(c) ((chart){one_c:(c)})
    >> #define ascii(x) as_chart((uint8_t)(x))
    >> #define CR 13
    >> #define LF 10
    >>
    >> static void wr_ch_unbuffered_dos (void) {
    >> static chart const crlf[2] = { ascii(CR), ascii(LF) };
    >> for (int i = 0; i < 2; i++)
    >> printf("%d\n",crlf.one_c);
    >> }
    >>
    >> int main (int argc, char *argv[]) {
    >> printf("%d %s\n",argc,argv[0]);
    >> wr_ch_unbuffered_dos();
    >> return 0;
    >> }
    >> ====================================================
    >> cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
    >> cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
    >> cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
    >> cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
    >> cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
    >>
    >> the bad line is
    >> static chart const crlf[2] = { ascii(CR), ascii(LF) };
    >>
    >> I am pretty sure code like this worked with g++ 3.
    >> What do I do now?
    >> Thanks!
    >>
    >> $ g++ --version
    >> g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)

    >
    > Looks like you're trying to use C99 features that C++ does not support.
    > Look for a compiler flag to enable those extensions, or change your
    > code to valid C++ syntax.


    what would be "valid C++ syntax" in this case?

    --
    Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
    http://honestreporting.com http://camera.org http://thereligionofpeace.com
    http://pmw.org.il http://memri.org http://israelunderattack.slide.com
    A poet who reads his verse in public may have other nasty habits.
     
    Sam Steingold, Sep 27, 2006
    #3
  4. Sam Steingold wrote:
    >> * mlimber <> [2006-09-27 07:40:58 -0700]:
    >>
    >> Sam Steingold wrote:
    >>> the following program does not compile with g++ 4.1.1:
    >>> ====================================================
    >>> // $Id$
    >>> // $Source$
    >>>
    >>> #include <stdint.h>
    >>> #include <stdio.h>
    >>>
    >>> typedef struct { uint32_t one_c; } chart;
    >>> #define as_chart(c) ((chart){one_c:(c)})
    >>> #define ascii(x) as_chart((uint8_t)(x))
    >>> #define CR 13
    >>> #define LF 10
    >>>
    >>> static void wr_ch_unbuffered_dos (void) {
    >>> static chart const crlf[2] = { ascii(CR), ascii(LF) };
    >>> for (int i = 0; i < 2; i++)
    >>> printf("%d\n",crlf.one_c);
    >>> }
    >>>
    >>> int main (int argc, char *argv[]) {
    >>> printf("%d %s\n",argc,argv[0]);
    >>> wr_ch_unbuffered_dos();
    >>> return 0;
    >>> }
    >>> ====================================================
    >>> cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
    >>> cpp-struct.cc:14: warning: missing braces around initializer for
    >>> 'const chart' cpp-struct.cc:14: warning: missing braces around
    >>> initializer for 'const chart' cpp-struct.cc:14: error: cannot
    >>> convert 'chart' to 'uint32_t' in initialization cpp-struct.cc:14:
    >>> error: cannot convert 'chart' to 'uint32_t' in initialization
    >>>
    >>> the bad line is
    >>> static chart const crlf[2] = { ascii(CR), ascii(LF) };
    >>>
    >>> I am pretty sure code like this worked with g++ 3.
    >>> What do I do now?
    >>> Thanks!
    >>>
    >>> $ g++ --version
    >>> g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)

    >>
    >> Looks like you're trying to use C99 features that C++ does not
    >> support. Look for a compiler flag to enable those extensions, or
    >> change your code to valid C++ syntax.

    >
    > what would be "valid C++ syntax" in this case?


    Not sure what you're trying to accomplish with it, but on
    a 32-bit machine, it's something like

    // there is no <stdint.h> in C++, but here is the approximation
    typedef unsigned uint32_t;
    typedef unsigned char uint8_t;

    #include <stdio.h>

    struct chart { uint32_t one_c; };

    #define as_chart(c) (c)
    #define ascii(x) as_chart(uint8_t(x))
    #define CR 13
    #define LF 10

    static void wr_ch_unbuffered_dos (void) {
    static chart const crlf[2] = { ascii(CR), ascii(LF) };
    for (int i = 0; i < 2; i++)
    printf("%d\n",crlf.one_c);
    }

    int main (int argc, char *argv[]) {
    printf("%d %s\n",argc,argv[0]);
    wr_ch_unbuffered_dos();
    return 0;
    }

    Note, however, that 'chart' should probably have a constructor,
    but since it changes its nature, adding a c-tor wasn't done.
    You need to state your intentions and then we can help you find
    a proper C++ solution.

    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, Sep 27, 2006
    #4
  5. Sam Steingold

    mlimber Guest

    Sam Steingold wrote:
    > > * mlimber <> [2006-09-27 07:40:58 -0700]:
    > >
    > > Sam Steingold wrote:
    > >> the following program does not compile with g++ 4.1.1:
    > >> ====================================================
    > >> // $Id$
    > >> // $Source$
    > >>
    > >> #include <stdint.h>
    > >> #include <stdio.h>
    > >>
    > >> typedef struct { uint32_t one_c; } chart;
    > >> #define as_chart(c) ((chart){one_c:(c)})
    > >> #define ascii(x) as_chart((uint8_t)(x))
    > >> #define CR 13
    > >> #define LF 10
    > >>
    > >> static void wr_ch_unbuffered_dos (void) {
    > >> static chart const crlf[2] = { ascii(CR), ascii(LF) };
    > >> for (int i = 0; i < 2; i++)
    > >> printf("%d\n",crlf.one_c);
    > >> }
    > >>
    > >> int main (int argc, char *argv[]) {
    > >> printf("%d %s\n",argc,argv[0]);
    > >> wr_ch_unbuffered_dos();
    > >> return 0;
    > >> }
    > >> ====================================================
    > >> cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
    > >> cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
    > >> cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
    > >> cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
    > >> cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
    > >>
    > >> the bad line is
    > >> static chart const crlf[2] = { ascii(CR), ascii(LF) };
    > >>
    > >> I am pretty sure code like this worked with g++ 3.
    > >> What do I do now?
    > >> Thanks!
    > >>
    > >> $ g++ --version
    > >> g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)

    > >
    > > Looks like you're trying to use C99 features that C++ does not support.
    > > Look for a compiler flag to enable those extensions, or change your
    > > code to valid C++ syntax.

    >
    > what would be "valid C++ syntax" in this case?


    Well, technically stdint.h isn't part of C++, but assuming that
    extension exists, you could make your code:

    #include <stdint.h>
    #include <stdio.h>

    typedef struct { uint32_t one_c; } chart;
    //#define as_chart(c) ((chart){one_c:(c)}) // Unneeded
    //#define ascii(x) as_chart((uint8_t)(x)) // Unneeded
    #define CR 13
    #define LF 10

    static void wr_ch_unbuffered_dos (void) {
    static chart const crlf[2] = { {CR}, {LF} }; // Change this
    for (int i = 0; i < 2; i++)
    printf("%u\n",crlf.one_c); // Note format string
    }

    Of course, if you were going all C++, I'd make some other changes
    (viz., convert the typedef to an ordinary struct definition, get rid of
    the macros, drop the "static" which has been deprecated in favor of
    anonymous namespaces, use iostreams instead of printf [cf.
    http://www.parashift.com/c -faq-lite/input-output.html#faq-15.1], and
    drop the "void" in parentheses).

    Cheers! --M
     
    mlimber, Sep 27, 2006
    #5
  6. Sam Steingold

    mlimber Guest

    mlimber wrote:
    > Of course, if you were going all C++, I'd make some other changes
    > (viz., convert the typedef to an ordinary struct definition, get rid of
    > the macros, drop the "static" which has been deprecated in favor of
    > anonymous namespaces, use iostreams instead of printf [cf.
    > http://www.parashift.com/c -faq-lite/input-output.html#faq-15.1], and
    > drop the "void" in parentheses).


    Oh, and add a constructor like Victor said.

    Cheers! --M
     
    mlimber, Sep 27, 2006
    #6
  7. > * Victor Bazarov <> [2006-09-27 11:44:39 -0400]:
    >
    > Sam Steingold wrote:
    >>> * mlimber <> [2006-09-27 07:40:58 -0700]:
    >>>
    >>> Sam Steingold wrote:
    >>>> the following program does not compile with g++ 4.1.1:
    >>>> ====================================================
    >>>> // $Id$
    >>>> // $Source$
    >>>>
    >>>> #include <stdint.h>
    >>>> #include <stdio.h>
    >>>>
    >>>> typedef struct { uint32_t one_c; } chart;
    >>>> #define as_chart(c) ((chart){one_c:(c)})
    >>>> #define ascii(x) as_chart((uint8_t)(x))
    >>>> #define CR 13
    >>>> #define LF 10
    >>>>
    >>>> static void wr_ch_unbuffered_dos (void) {
    >>>> static chart const crlf[2] = { ascii(CR), ascii(LF) };
    >>>> for (int i = 0; i < 2; i++)
    >>>> printf("%d\n",crlf.one_c);
    >>>> }
    >>>>
    >>>> int main (int argc, char *argv[]) {
    >>>> printf("%d %s\n",argc,argv[0]);
    >>>> wr_ch_unbuffered_dos();
    >>>> return 0;
    >>>> }
    >>>> ====================================================
    >>>> cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
    >>>> cpp-struct.cc:14: warning: missing braces around initializer for
    >>>> 'const chart' cpp-struct.cc:14: warning: missing braces around
    >>>> initializer for 'const chart' cpp-struct.cc:14: error: cannot
    >>>> convert 'chart' to 'uint32_t' in initialization cpp-struct.cc:14:
    >>>> error: cannot convert 'chart' to 'uint32_t' in initialization
    >>>>
    >>>> the bad line is
    >>>> static chart const crlf[2] = { ascii(CR), ascii(LF) };
    >>>>
    >>>> I am pretty sure code like this worked with g++ 3.
    >>>> What do I do now?
    >>>> Thanks!
    >>>>
    >>>> $ g++ --version
    >>>> g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)


    > Not sure what you're trying to accomplish with it,


    CLISP (http://clisp.cons.org) is normally compiled with C, but it should
    be compilable with C++ to enable some compile-time as well as run-time
    checks for debugging. it has always compiled with g++, but I cannot
    compile it with g++ 4.1.1 - specifically, I get the errors mentioned above.

    note that if I replace

    #define as_chart(c) ((chart){one_c:(c)})

    with

    extern __inline__ chart as_chart (register cint c)
    { register chart ch; ch.one_c = c; return ch; }

    it becomes compilable with g++ 4.1

    note also that it is only nested initializations that do not work.

    chart foo = ascii(60);

    works just fine.


    so, I should have formulated my question like this: how do I modify
    as_chart so that g++ 4.1.1 will accept it?

    I am not interested in C++ bells and whistles here (they ARE used
    elsewhere, but I do not need them at this place), all I care is how to
    make the code compile.

    thanks for your kind help/

    --
    Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
    http://dhimmi.com http://memri.org http://openvotingconsortium.org
    http://iris.org.il http://jihadwatch.org http://israelnorthblog.livejournal.com
    Money does not "play a role", it writes the scenario.
     
    Sam Steingold, Sep 27, 2006
    #7
  8. Sam Steingold

    mlimber Guest

    Sam Steingold wrote:
    > CLISP (http://clisp.cons.org) is normally compiled with C, but it should
    > be compilable with C++ to enable some compile-time as well as run-time
    > checks for debugging. it has always compiled with g++, but I cannot
    > compile it with g++ 4.1.1 - specifically, I get the errors mentioned above.
    >
    > note that if I replace
    >
    > #define as_chart(c) ((chart){one_c:(c)})


    This is not valid C++ syntax. If g++ 3.x supported it, it was by
    extension.

    >
    > with
    >
    > extern __inline__ chart as_chart (register cint c)
    > { register chart ch; ch.one_c = c; return ch; }


    Extern and inline? BTW, __inline__ is non-standard, and using the
    register keyword may in fact just confuse the optimizer. Best to leave
    that off.

    > it becomes compilable with g++ 4.1
    >
    > note also that it is only nested initializations that do not work.
    >
    > chart foo = ascii(60);
    >
    > works just fine.
    >
    >
    > so, I should have formulated my question like this: how do I modify
    > as_chart so that g++ 4.1.1 will accept it?


    Get rid of the one_c:{c} business. That's the problem.

    > I am not interested in C++ bells and whistles here (they ARE used
    > elsewhere, but I do not need them at this place), all I care is how to
    > make the code compile.


    See my other post.

    Cheers! --M
     
    mlimber, Sep 27, 2006
    #8
  9. > * mlimber <> [2006-09-27 09:16:44 -0700]:
    >
    >> so, I should have formulated my question like this: how do I modify
    >> as_chart so that g++ 4.1.1 will accept it?

    >
    > Get rid of the one_c:{c} business. That's the problem.


    I don't have "one_c:{c}".
    I have "((chart){one_c:(c)})".
    what do I replace it with?
    a constructor?
    how do I write it?
    thanks.

    >> I am not interested in C++ bells and whistles here (they ARE used
    >> elsewhere, but I do not need them at this place), all I care is how to
    >> make the code compile.

    >
    > See my other post.


    which one?

    --
    Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
    http://palestinefacts.org http://ffii.org http://israelunderattack.slide.com
    http://pmw.org.il http://thereligionofpeace.com http://truepeace.org
    If abortion is murder, then oral sex is cannibalism.
     
    Sam Steingold, Sep 27, 2006
    #9
  10. Sam Steingold

    mlimber Guest

    mlimber, Sep 27, 2006
    #10
  11. Sam Steingold

    Rolf Magnus Guest

    Sam Steingold wrote:

    >> * mlimber <> [2006-09-27 09:16:44 -0700]:
    >>
    >>> so, I should have formulated my question like this: how do I modify
    >>> as_chart so that g++ 4.1.1 will accept it?

    >>
    >> Get rid of the one_c:{c} business. That's the problem.

    >
    > I don't have "one_c:{c}".
    > I have "((chart){one_c:(c)})".



    Seems to me like the same, just with a cast in front of it.

    > what do I replace it with?


    That depends on what it's supposed to do.
     
    Rolf Magnus, Sep 27, 2006
    #11
  12. > * Rolf Magnus <> [2006-09-27 19:24:54 +0200]:
    >
    > Sam Steingold wrote:
    >
    >>> * mlimber <> [2006-09-27 09:16:44 -0700]:
    >>>
    >>>> so, I should have formulated my question like this: how do I modify
    >>>> as_chart so that g++ 4.1.1 will accept it?
    >>>
    >>> Get rid of the one_c:{c} business. That's the problem.

    >>
    >> what do I replace it with?

    >
    > That depends on what it's supposed to do.


    convert an integer to a structure:

    typedef struct { int one_c; } chart;
    #define as_chart(c) ((chart){one_c:(c)})

    as_chart should convert an integer to a struct.


    --
    Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
    http://iris.org.il http://mideasttruth.com http://israelunderattack.slide.com
    http://honestreporting.com http://dhimmi.com http://jihadwatch.org
    The world is coming to an end. Please log off.
     
    Sam Steingold, Sep 27, 2006
    #12
  13. Sam Steingold

    Nate Barney Guest

    Sam Steingold wrote:
    >> * Rolf Magnus <> [2006-09-27 19:24:54 +0200]:
    >>
    >> Sam Steingold wrote:
    >>
    >>>> * mlimber <> [2006-09-27 09:16:44 -0700]:
    >>>>
    >>>>> so, I should have formulated my question like this: how do I modify
    >>>>> as_chart so that g++ 4.1.1 will accept it?
    >>>> Get rid of the one_c:{c} business. That's the problem.
    >>> what do I replace it with?

    >> That depends on what it's supposed to do.

    >
    > convert an integer to a structure:
    >
    > typedef struct { int one_c; } chart;
    > #define as_chart(c) ((chart){one_c:(c)})
    >
    > as_chart should convert an integer to a struct.
    >


    How about:

    struct chart { chart() {} chart(int c) : one_c(c) {} int one_c; };
    inline chart as_chart(int c) { return chart(c); }
     
    Nate Barney, Sep 27, 2006
    #13
  14. > * Nate Barney <> [2006-09-27 13:58:26 -0400]:
    >
    > Sam Steingold wrote:
    >>> * Rolf Magnus <> [2006-09-27 19:24:54 +0200]:
    >>>
    >>> Sam Steingold wrote:
    >>>
    >>>>> * mlimber <> [2006-09-27 09:16:44 -0700]:
    >>>>>
    >>>>>> so, I should have formulated my question like this: how do I modify
    >>>>>> as_chart so that g++ 4.1.1 will accept it?
    >>>>> Get rid of the one_c:{c} business. That's the problem.
    >>>> what do I replace it with?
    >>> That depends on what it's supposed to do.

    >>
    >> convert an integer to a structure:
    >>
    >> typedef struct { int one_c; } chart;
    >> #define as_chart(c) ((chart){one_c:(c)})
    >>
    >> as_chart should convert an integer to a struct.
    >>

    >
    > How about:
    >
    > struct chart { chart() {} chart(int c) : one_c(c) {} int one_c; };
    > inline chart as_chart(int c) { return chart(c); }


    this works, thanks!

    --
    Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
    http://mideasttruth.com http://palestinefacts.org http://jihadwatch.org
    http://truepeace.org http://israelnorthblog.livejournal.com http://iris.org.il
    You think Oedipus had a problem -- Adam was Eve's mother.
     
    Sam Steingold, Sep 27, 2006
    #14
    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. dorayme
    Replies:
    1
    Views:
    635
    richard
    Jan 21, 2011
  2. richard
    Replies:
    0
    Views:
    598
    richard
    Jan 21, 2011
  3. richard
    Replies:
    0
    Views:
    625
    richard
    Jan 21, 2011
  4. Beauregard T. Shagnasty

    Re: A Weird Appearance for a Weird Site

    Beauregard T. Shagnasty, Jan 21, 2011, in forum: HTML
    Replies:
    1
    Views:
    453
    Captain Paralytic
    Jan 21, 2011
  5. David Segall
    Replies:
    0
    Views:
    642
    David Segall
    Jan 22, 2011
Loading...

Share This Page