Why is this not allowed anymore (anymore being with a more recent compiler)?

E

Eric Lilja

So I tried to compile code that my boss wrote with a newer compiler
than I was previously using (FYI, the code compiles fine on g++ 3.4.4
but not on 4.1.1). I condensed it into this simple testprogram:

#include <string>

class Foo
{
private:
static const std::string LETTERS;
};

using namespace std;

namespace
{
const string Foo::LETTERS("abc");
}

int
main()
{
Foo f;

(void)f;
}

I must admit I'm not entirely sure what he want the anonymous (correct
term?) namespace for, but the more recent compiler sure doesn't like
it:
$ g++ --version
g++ (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
enclosing 'Foo'

I need you gurus on this one. :)

/ E
 
V

VJ

Eric said:
So I tried to compile code that my boss wrote with a newer compiler
than I was previously using (FYI, the code compiles fine on g++ 3.4.4
but not on 4.1.1). I condensed it into this simple testprogram:

#include <string>

class Foo
{
private:
static const std::string LETTERS;
};

using namespace std;

namespace
{
const string Foo::LETTERS("abc");
}

int
main()
{
Foo f;

(void)f;
}

I must admit I'm not entirely sure what he want the anonymous (correct
term?) namespace for, but the more recent compiler sure doesn't like

anonymous namespace is instead of using static variables
(variables/functions are only visible in the file where they are defined)
it:
$ g++ --version
g++ (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
enclosing 'Foo'

I need you gurus on this one. :)

/ E

Your program compiles for me without warnings and errors. My compiler is:
gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)
 
A

Alf P. Steinbach

* Eric Lilja:
So I tried to compile code that my boss wrote with a newer compiler
than I was previously using (FYI, the code compiles fine on g++ 3.4.4
but not on 4.1.1). I condensed it into this simple testprogram:

#include <string>

class Foo
{
private:
static const std::string LETTERS;
};

using namespace std;

namespace
{
const string Foo::LETTERS("abc");
}

int
main()
{
Foo f;

(void)f;
}

I must admit I'm not entirely sure what he want the anonymous (correct
term?) namespace for, but the more recent compiler sure doesn't like
it:
$ g++ --version
g++ (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
enclosing 'Foo'

I need you gurus on this one. :)

§9.4.2/2 "The definition of a static data member shall appear in a
namespace scope enclosing the member's class definition" (the same rule
applies to member functions).

In the above code, the static data member is defined in a namespace that
does not enclose the class definition.

The anonymous namespace is just silly and incorrect the way it's used
above, but you could put the class definition inside. Also, in C and
C++ preferentially reserve all uppercase names for macros (is your boss
a former Java programmer?). Using all uppercase you (or your boss) have
no reason to complain if some header file defines a macro LETTERS that
interferes destructively with the code.
 
D

Daniel T.

Eric Lilja said:
So I tried to compile code that my boss wrote with a newer compiler
than I was previously using (FYI, the code compiles fine on g++ 3.4.4
but not on 4.1.1). I condensed it into this simple testprogram:

#include <string>

class Foo
{
private:
static const std::string LETTERS;
};

using namespace std;

namespace
{
const string Foo::LETTERS("abc");
}

int
main()
{
Foo f;

(void)f;
}

I must admit I'm not entirely sure what he want the anonymous (correct
term?) namespace for, but the more recent compiler sure doesn't like
it:
$ g++ --version
g++ (GCC) 4.1.1
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ g++ -Wall -Wextra -std=c++98 -pedantic foo.cpp -o runme.exe
foo.cpp:13: error: definition of 'Foo::LETTERS' is not in namespace
enclosing 'Foo'

I need you gurus on this one. :)

It doesn't look like it should compile. LETTERS is declared as a member
of Foo which is in global scope but never defined as such; instead it's
defined in anonymous namespace scope.
 
V

VJ

VJ said:
Eric Lilja wrote:

Reading other posts, I just realized that he put the definition of the
static variable inside anonymous namespace. If class Foo was declared in
another namespace, this would not compile at all
 

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

Forum statistics

Threads
473,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top