[cfe-dev] "using namespace" seemingly not effective
David Blaikie
dblaikie at gmail.com
Sun May 12 09:54:46 PDT 2013
On Sun, May 12, 2013 at 9:49 AM, Florian Weimer <fweimer at redhat.com> wrote:
> On 05/12/2013 06:24 PM, David Blaikie wrote:
>>
>> On Sun, May 12, 2013 at 12:47 AM, Florian Weimer <fweimer at redhat.com>
>> wrote:
>>>
>>> clang++ (default flags, trunk version) does not grok the following code:
>>>
>>> namespace outer {
>>> void f(int &);
>>> void g();
>>> namespace inner {
>>> void f(long &);
>>> }
>>> }
>>>
>>> void
>>> outer::g()
>>> {
>>> using namespace outer::inner;
>>> int i;
>>> f(i);
>>> long l;
>>> f(l);
>>> }
>>>
>>> t.cpp:16:3: error: no matching function for call to 'f'
>>> f(l);
>>> ^
>>> t.cpp:2:8: note: candidate function not viable: no known conversion from
>>> 'long' to 'int &' for 1st argument
>>> void f(int &);
>>> ^
>>>
>>> I find this rather odd, considering that there is an explicit "using
>>> namespace" directive. I'm not sufficiently familiar with the C++ name
>>> lookup rules, but I could have understood if f(int &) became invisible.
>>> But
>>> the other way round is very surprising.
>>
>>
>> (this isn't really a C++ language support forum, but anyway)
>>
>> Notice that the second call does work - so clearly something about the
>> name lookup is working. And the first call that errors points directly
>> to the candidate you intended to call (via the using directive) but
>> explains why it can't call that version.
>
>
> Uhm, please look again at the example code. It's the *second* call that
> fails, and the using directive should bring that overload into scope.
Hmm, right you are - shows how well I can read/think at this hour on a
Saturday morning. Apologies for that.
GCC 4.7 compiles this code without error & I tend to agree with it so
far as I can imagine, though there might be some subtle feature of the
language I'm missing here (that's an appropriate caveat with pretty
much anything in C++ though). I'd say it's worthy of a PR and/or some
other further investigation.
More information about the cfe-dev
mailing list