Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How does the name lookup work in this case?

Reply
Thread Tools

How does the name lookup work in this case?

 
 
Peter
Guest
Posts: n/a
 
      01-28-2014
Consider this definition (namespace and class share the same name):

namespace Foo
{
int x;

class Foo
{
public:
static int x;
};

int Foo:;
}

I wondered what Foo: would refer to with "using" directive used for namespace Foo: a global variable x in namespace Foo or static member of class Foo? Basically, I assumed the following code would not compile:

int main()
{
using namespace Foo;
Foo:;
return 0;
}

My reasoning went like this:

- Foo: is a global variable x from namespace Foo
- Foo::Foo: is a static member of class Foo from namespace Foo, but since
"using" directive is applied, the namespace name can be omitted, thus Foo: is also a static member of class Foo
- conclusion: call to Foo: in main() is ambiguous - it refers to two different entities

However, the compiler I tested it with (one of g++ recent versions) had no trouble disambiguating this: experiments showed Foo: in main() is interpreted as global variable x in namespace Foo. Moreover, if I remove the definition of global x from namespace Foo, then the compiler emits the followingerror:

main.cpp: In function 'int main()':
main.cpp:16:4: error: 'x' is not a member of 'Foo'
Foo:;

so it doesn't find the static member of class Foo. In order for the compiler to find it I have to qualify it fully as Foo::Foo: despite the "using namespace Foo;" line. Why? How does the lookup work here?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-28-2014
On 28.01.2014 21:47, Peter wrote:
> Consider this definition (namespace and class share the same name):
>
> namespace Foo
> {
> int x;
>
> class Foo
> {
> public:
> static int x;
> };
>
> int Foo:;
> }
>
> I wondered what Foo: would refer to with "using" directive used for namespace Foo: a global variable x in namespace Foo or static member of class Foo? Basically, I assumed the following code would not compile:
>
> int main()
> {
> using namespace Foo;
> Foo:;
> return 0;
> }
>
> My reasoning went like this:
>
> - Foo: is a global variable x from namespace Foo
> - Foo::Foo: is a static member of class Foo from namespace Foo, but since
> "using" directive is applied, the namespace name can be omitted, thus Foo: is also a static member of class Foo
> - conclusion: call to Foo: in main() is ambiguous - it refers to two different entities


I think that is correct.

And I think probably C++11 §7.3.4/6, in the "Using directive" section,
applies:

"If name lookup finds a declaration for a name in two different
namespaces, and the declarations do not declare the same entity and do
not declare functions, the use of the name is ill-formed."

As it happens Visual C++ 12.0 appears to also think so:

[error]
original.cpp(19) : error C2872: 'Foo' : ambiguous symbol
could be 'Foo'
or 'original.cpp( : Foo::Foo'
[/error]


> However, the compiler I tested it with (one of g++ recent versions) had
> no trouble disambiguating this: experiments showed Foo: in main() is
> interpreted as global variable x in namespace Foo. Moreover, if I remove
> the definition of global x from namespace Foo, then the compiler emits
> the following error:
>
> main.cpp: In function 'int main()':
> main.cpp:16:4: error: 'x' is not a member of 'Foo'
> Foo:;
>
> so it doesn't find the static member of class Foo.


Except that I believe that's wrong, it's reasonable: maybe its how the
language should be.


> In order for the compiler to find it I have to qualify it fully as
> Foo::Foo: despite the "using namespace Foo;" line.


That's not full qualification, and Visual C++ still emits the diagnostic
above.

Full qualification is

::Foo::Foo:;

and this is accepted by Visual C++.


> Why?


I think the ambiguity is real and that the g++ failure to diagnose it is
a compiler bug. I could be wrong. But, since the two compilers disagree,
at least one of Visual C++ and g++ has a bug here.


> How does the lookup work here?


C++11 §7.3.4/2

"A using-directive specifies that the names in the nominated namespace
can be used in the scope in which the using-directive appears after the
using-directive. During unqualified name lookup (3.4.1), the names
appear as if they were declared in the nearest enclosing namespace which
contains both the using-directive and the nominated namespace"


Cheers & hth.,

- Alf

 
Reply With Quote
 
 
 
 
Peter
Guest
Posts: n/a
 
      01-30-2014
> > In order for the compiler to find it I have to qualify it fully as
>
> > Foo::Foo: despite the "using namespace Foo;" line.

>
>
>
> That's not full qualification, and Visual C++ still emits the diagnostic
>
> above.
>
>
>
> Full qualification is
>
>
>
> ::Foo::Foo:;
>
>
>
> and this is accepted by Visual C++.
>


Perhaps a silly question, but what's the difference
between ::Foo::Foo: and Foo::Foo: here?
I thought they were equal in this context.

> I think the ambiguity is real and that the g++ failure to diagnose it is
>
> a compiler bug. I could be wrong. But, since the two compilers disagree,
>
> at least one of Visual C++ and g++ has a bug here.
>


I also think this may be a g++ bug (unless there's yet another
obscure C++ rule according to which the compiler isn't supposed to
interpret Foo: as static member of class Foo in this case).


 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      01-31-2014
On Thursday, 30 January 2014 20:09:52 UTC+2, Peter wrote:

Attribute quotes, please.

>On Tuesday, 28 January 2014 23:32:52 UTC+2, Alf P. Steinbach wrote:
> > Full qualification is
> >
> > ::Foo::Foo:;
> >

>
> Perhaps a silly question, but what's the difference
> between ::Foo::Foo: and Foo::Foo: here?
> I thought they were equal in this context.


The difference is what Alf said, one is fully qualified name
and other is not. Other 'x' in context of your question is
'::Foo:' when fully qualified.
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      01-31-2014
Getting back to the main point of the thread...
as Alf pointed out, since g++ and Visual C++
respond to my code differently, then at least
one of them handles it wrong. Any opinions
on which (if any) is correct?

Here are the g++ versions I tested the code with online:

http://www.compileonline.com/compile_cpp11_online.php (g++ 4.7.2, in C++11 compliant mode)

http://www.compileonline.com/compile_cpp_online.php (g++ 4.8.1)
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      01-31-2014
On Friday, 31 January 2014 21:52:57 UTC+2, Peter wrote:
> Getting back to the main point of the thread...
> as Alf pointed out, since g++ and Visual C++
> respond to my code differently, then at least
> one of them handles it wrong. Any opinions
> on which (if any) is correct?


My opinion is that usage of unqualified name 'Foo' after doing
'using namespace Foo;' that contains other 'Foo' is ambiguous.
So even when one of the '::Foo::Foo:' or '::Foo:' is not
available it should not compile. I'm not sure how gcc looks it
up. Perhaps it tries to be overly smart somewhere.

I tried with clang, it did also reject the code.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to break a bash command into an array consisting of the argumentsin the command? Peng Yu Perl Misc 3 05-13-2013 10:27 AM
Why sfml does not play the file inside a function in this python code? cheirasacan@gmail.com Python 6 05-07-2013 08:02 PM
Lightroom Export, am I doing this the hard way? J. Clarke Digital Photography 0 05-05-2013 08:15 PM
How do I encode and decode this data to write to a file? cl@isbd.net Python 11 05-01-2013 11:36 PM
How to capture the “same” RGB images for the same scene using EDSDK? azuring@gmail.com Digital Photography 0 04-28-2013 12:08 PM



Advertisments