compiler warning generated for extern definition

S

subramanian100in

Consider the following program x.cpp:

#include <cstdlib>
#include <iostream>

using namespace std;

namespace Sample
{
extern int sampleInt = 100;
}

using namespace Sample;

extern int globalInt = 200;

int main()
{
cout << sampleInt << endl;
cout << globalInt << endl;

return EXIT_SUCCESS;
}

When I compile this program using g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
I get the following compiler warnings:
x.cpp:8: warning: `sampleInt' initialized and declared `extern'
x.cpp:13: warning: `globalInt' initialized and declared `extern'

I read in "C++ Primer 4th edition" by Stanley Lippman (page 53) that
"An extern declaration may include an initializer only if it appears
outside a function and an extern that is initialized is treated as a
definition".

If this is so, why does the g++3.4.3 compiler generate warning
messages ? Is my program wrong ?

Kindly explain.

Thanks
V.Subramanian
 
J

James Kanze

The standard requires "a diagnostic" for violations of the
language requirements. Every compiler I know of documents that
any warning or error is "a diagnostic".
That may have missed your point, which may have been that if
the compiler "only" gives a warning, then there's nothing
actually wrong with the code. This gives compilers far more
credit than I'm willing to allow them.
Or maybe your point was that in this case, the warning doesn't
indicate an actual problem. In that case, though, since the
"extern" is redundant, it should be removed.

Maybe. If the variables were const, the "extern" would be
necessary. One could imagine a convention which required it,
on the grounds of consistency. (And hopefully the compiler
doesn't warn if this case occurs on const variables.)
 
J

James Kanze

IOW, if there is a diagnostic (any message), it can be because
the language requires is (if there is a violation). But the
inverse is not necessarily true, yes? I.e. if there are no
violations, it does not mean there will be no diagnostics
since the compilers can issue any number of them at any point.
Yes? No? Maybe?

Correct. A compiler can simply output a question mark every
time it compiles something, and document that as a "diagnostic",
and it's conformant. And once a diagnostic is output, if the
code is illegal, the compiler is free to do whatever it likes.

Quality of implementation concerns demand a bit more.
Hm... At the same time, even if the diagnostic is issued, the
compiler is allowed to compile the code and output a working
(whatever that may mean) program, yes?

Formally, once a diagnostic has been output, the rest is
undefined behavior. A compiler could document that the
diagnostic was "the light on your hard disk turns on for x
seconds per Gigabyte", and format the hard disk.

Again, QoI is a bit more restrictive. In practice, we expect
not to get an executable if there is an error. Most compilers
sort of approximate this (but might give you an executable if
you're using an extension, or for things where they disagree
somewhat with the standard, and think should be legal).

Or the warning removed from the compiler. I really can't image
a case where the extra extern would cause a problem, either at
execution or for a maintenance programmer.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top