weird warning with g++ 4.1

S

Sam Steingold

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)
 
M

mlimber

Sam said:
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
 
S

Sam Steingold

* mlimber said:
Sam said:
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?
 
V

Victor Bazarov

Sam said:
* mlimber <[email protected]> [2006-09-27 07:40:58 -0700]:

Sam said:
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
 
M

mlimber

Sam said:
* mlimber <[email protected]> [2006-09-27 07:40:58 -0700]:

Sam said:
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
 
S

Sam Steingold

* Victor Bazarov said:
Sam said:
* mlimber <[email protected]> [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/
 
M

mlimber

Sam said:
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
 
S

Sam Steingold

* mlimber said:
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.
See my other post.

which one?
 
S

Sam Steingold

* Rolf Magnus said:
Sam said:
* mlimber <[email protected]> [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.
 
N

Nate Barney

Sam said:
* Rolf Magnus <[email protected]> [2006-09-27 19:24:54 +0200]:

Sam said:
* mlimber <[email protected]> [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); }
 
S

Sam Steingold

* Nate Barney said:
Sam said:
* Rolf Magnus <[email protected]> [2006-09-27 19:24:54 +0200]:

Sam Steingold wrote:

* mlimber <[email protected]> [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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top