Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

weird warning with g++ 4.1

0 views
Skip to first unread message

Sam Steingold

unread,
Sep 27, 2006, 8:49:59 AM9/27/06
to
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[i].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.

mlimber

unread,
Sep 27, 2006, 10:40:58 AM9/27/06
to

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

Sam Steingold

unread,
Sep 27, 2006, 11:11:12 AM9/27/06
to mlimber
> * mlimber <zyv...@tznvy.pbz> [2006-09-27 07:40:58 -0700]:

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.

Victor Bazarov

unread,
Sep 27, 2006, 11:44:39 AM9/27/06
to

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[i].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


mlimber

unread,
Sep 27, 2006, 11:50:28 AM9/27/06
to

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[i].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

unread,
Sep 27, 2006, 11:52:33 AM9/27/06
to
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

Sam Steingold

unread,
Sep 27, 2006, 12:03:29 PM9/27/06
to Victor Bazarov
> * Victor Bazarov <i.Non...@pbzNpnfg.arg> [2006-09-27 11:44:39 -0400]:

> 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.

mlimber

unread,
Sep 27, 2006, 12:16:44 PM9/27/06
to
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

Sam Steingold

unread,
Sep 27, 2006, 12:38:48 PM9/27/06
to mlimber
> * mlimber <zyv...@tznvy.pbz> [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.

mlimber

unread,
Sep 27, 2006, 12:43:48 PM9/27/06
to
Sam Steingold wrote:
> > * mlimber <zyv...@tznvy.pbz> [2006-09-27 09:16:44 -0700]:
> > See my other post.
>
> which one?

This one:

http://groups.google.com/group/comp.lang.c++/msg/d5dd44249bffc950

Cheers! --M

Rolf Magnus

unread,
Sep 27, 2006, 1:24:54 PM9/27/06
to
Sam Steingold wrote:

>> * mlimber <zyv...@tznvy.pbz> [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.

Sam Steingold

unread,
Sep 27, 2006, 1:40:41 PM9/27/06
to Rolf Magnus
> * Rolf Magnus <enzn...@g-bayvar.qr> [2006-09-27 19:24:54 +0200]:

>
> Sam Steingold wrote:
>
>>> * mlimber <zyv...@tznvy.pbz> [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.

Nate Barney

unread,
Sep 27, 2006, 1:58:26 PM9/27/06
to
Sam Steingold wrote:
>> * Rolf Magnus <enzn...@g-bayvar.qr> [2006-09-27 19:24:54 +0200]:
>>
>> Sam Steingold wrote:
>>
>>>> * mlimber <zyv...@tznvy.pbz> [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); }

Sam Steingold

unread,
Sep 27, 2006, 4:10:53 PM9/27/06
to Nate Barney
> * Nate Barney <angro...@tznvy.pbz> [2006-09-27 13:58:26 -0400]:

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.

0 new messages