C++ Logo

sg20

Advanced search

Re: namespace composition

From: Bjarne Stroustrup <bjarne_at_[hidden]>
Date: Fri, 28 Apr 2023 20:40:44 -0400
On 4/28/2023 7:52 PM, Ville Voutilainen wrote:
> On Sat, 29 Apr 2023 at 02:46, Bjarne Stroustrup <bjarne_at_[hidden]> wrote:
>> One of the first things I tried was this
>>
>>
>> import std;
>>
>> namespace PPP {
>> using namespace std;
>> struct vector { void found_our_own() {} };
>> }
>>
>> using namespace PPP;
> This is the problem; you can't do that using-directive.
>
>> int main() {
>> string s;
>> vector v;
>> v.found_our_own();
>> }
>>
>>
>> It would have been ideal for my purposes. I get a double definition of
>> vector error. language problem? My confusion? Compiler bug?
> The compiler implements the language correctly here. You *do* have two
> vectors in namespace PPP,
> but lookup will prefer the one you defined or user-declared in that
> namespace, if you use qualified names. If you bring every name in PPP
> into another scope, it'll bring both vectors into scope, and then
> that's ambiguous.

I know. Then I tried:

namespace PPP {
      using namespace std;
      struct My_vector { void found_our_own() {} };
 using vector = My_vector;
}

and that's when I got a bit surprised. And started to look for alternatives (with modules and without #include or #define).

>
> Name lookup in C++ is complicated. :P
Too complicated, and there is probably little we can do about it.

Received on 2023-04-29 00:40:47