On 10/13/2010 1:28 PM, Juha Nieminen wrote:
> James Kanze<> wrote:
>> On Oct 12, 5:37 pm, Juha Nieminen<nos...@thanks.invalid> wrote:
>>> Victor Bazarov<v.baza...@comcast.invalid> wrote:
>>>> Name lookup when in a member function finds
>>>> the member 'foo' which actually *hides* any other 'foo', and that's why
>>>> the '<unnamed namespace>::foo' should NOT be found during that lookup
>>>> and is *not* considered an overload.
>>
>>> What is the rationale for that? I can't immediately think of
>>> a reason or notivation for that.
>>
>> I suspect that the main reason is that that is how name lookup
>> always works. In just about every language which recognizes
>> scope. You wouldn't want to get a duplicate definition error
>> because you had defined an "int foo" at namespace scope, and
>> having the compiler find a function name, but not the name of
>> a variable, would be extremely confusing.
>
> A 'foo(int)' in an inner scope hiding a 'foo(int)' in the outer
> scope is understandable.
>
> However, a 'foo(int)' in the inner scope hiding a 'foo(int,int)'
> in the outer scope isn't. I don't understand why it has to be hidden,
> which is why I asked for the rationale for that.
The *rule* is that the *name* is looked up first, without the arguments.
That's how name lookup works. That's why the *name* alone hides
another name that is spelled the same way, regardless of *what the name
is of*.
> What would be the
> problem with an unqualified call like "foo(1, 2)" calling the function
> in the outer scope? It's not like it can be confused with the function
> in the inner scope (because the amount of parameters is clearly
> different).
So, what's important, the number of arguments or the number and the
types? Should 'foo(char, vector<int>*)' *hide* 'foo(int,int)' or
shouldn't it?
>> Beyond that, of course, you really don't want the compiler
>> looking further than you expect. Consider for a moment:
>>
>> class C
>> {
>> void f(int);
>> public:
>> void g() { f('a'); }
>> };
>>
>> When you wrote the class, it's obvious (I suppose) that you
>> intended g to call C::f. And you would be most annoyed if it
>> called some ::f some of the time (depending on what headers were
>> included before this header), but not others. Similarly, if
>> C derived from some other class, you wouldn't like the fact that
>> the author of that class added a private f(char) broke your
>> code.
>
> In this example the function call would match two function
> declarations, in which case the innermost scoped declaration wins.
> However, if the choices are 'foo(int)' and 'foo(int,int)', what
> is the reason to make the inner declaration hide the outer one?
The name lookup is already quite complicated. Mix in the arguments (the
number, but not the types), and you get plenty of problems that are
likely impossible to solve in a working compiler for all people to be
satisfied, I would guess.
It's easier for the language to be as strict as possible, but not
stricter. Making rules relaxed creates more problems than it solves.
V
--
I do not respond to top-posted replies, please don't ask
|