Static member of class not found if class name == name of namespace it's defined in

Skip to first unread message


Mar 12, 2014, 5:30:23 PM3/12/14
Some time ago I started a topic on comp.lang.c++
about possible bug in g++ found by me. Although there was
no general agreement as to whether it's a compiler bug or language
underspecification, there was a conclusion that something was
definitely wrong with my code example since g++ and Visual C++
handled it differently. I also sent a bug report to g++ bugzilla
more than a month ago, but no one seemed to care
as I receved no reply and bug status hasn't changed.

Consider this definition:

namespace Foo
int x;

class Foo
static int x;

int Foo::x;

and test code in which it's used:

int main()
using namespace Foo;
return 0;

Both g++ 4.7.2 and g++ 4.8.1 accept the above code even
though Foo::x is ambiguous: could be either a global variable
in namespace Foo or static member of class Foo. Visual C++ 2013
rejects the code with "error C2872: 'Foo' : ambiguous symbol could
be 'Foo' or 'Foo::Foo'", g++ simply resolves Foo::x to global
variable x.

If I comment out the global variable x from namespace definition
then both g++ 4.7.2 and g++ 4.8.1 reject the code with
"error: 'x' is not a member of 'Foo'", so they fail to find
the static member of class Foo unless I qualify it as Foo::Foo::x
(which should not be necessary due to "using" directive).

What's your opinion? Personally I think Visual C++ is right
here. Since this is a group focused on C++ standard
compliance you can probably decide what's the correct way
a compiler should handle my example.

[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: ]


Mar 14, 2014, 2:23:07 PM3/14/14
It' a g++ bug
In this case Foo::x is ambiguos

to be in theme another bug is (g++ 4.6.3):


class A {
class B {};

bool operator ==(const A::B&,const A::B&) { return true; }

template <typename T>
class C {
class D {};

template <typename T>
bool operator==(const typename C<T>::D&,const typename C<T>::D&) {
return true; }

int main()
A::B() == A::B(); // OK
C<int>::D() == C<int>::D(); // error


I don't now the behavior of VC++ or the next release of g++
Reply all
Reply to author
0 new messages