g++ thingy

K

keith

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

Robert Fendt

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
 
K

keith

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?
 
V

Vaclav Haisman

Marcel Müller wrote, On 14.2.2010 18:24:
Why shouldn't it?
Because IIRC only integral types constants can be initialized at the
declaration like that.
 
A

Andrew Poelstra

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

Krice

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

keith

Krice pravi:
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.
 
R

Rolf Magnus

Pete said:
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.
 
H

Helge Kruse

keith said:
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
 
J

Jorgen Grahn


Not to mention -Wall -Wextra.
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
 
K

keith

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

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

Jorgen Grahn

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
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top