C++ Logo

sg20

Advanced search

Re: [isocpp-ext] namespace composition

From: Andrew Tomazos <andrewtomazos_at_[hidden]>
Date: Sat, 29 Apr 2023 08:23:04 +1000
Damn. I was hoping the AI was smarter than the creator of the programming
language its written in, and we could have had some kind of watershed
moment in history.

On Sat, Apr 29, 2023 at 8:19 AM Ville Voutilainen via Ext <
ext_at_[hidden]> wrote:

> On Sat, 29 Apr 2023 at 01:11, Ville Voutilainen
> <ville.voutilainen_at_[hidden]> wrote:
> > We can actually do such composition, but..
> > https://wandbox.org/permlink/8Av08StRKKBvAzgW
> > Pasted here for convenience:
> >
> > #include <string>
> > #include <iostream>
> > #include <vector>
> >
> > namespace PPP {
> > using namespace std;
> > struct vector {void found_our_own() {}};
> > }
> >
> > int main() {
> > PPP::string s;
> > PPP::vector v;
> > v.found_our_own();
> > }
> >
> > So, we can compose things in such a manner that when using qualified
> > names, i.e. PPP::vector, that works - PPP::string is std::string,
> > PPP::vector is the vector I defined. Unambiguously.
> >
> > But you can't do a using namespace PPP in main(), and then use string
> > and vector unqualified. In that case vector becomes an ambiguous
> > type.
>
> And yes, the most-likely-incorrect chatbot got it right too, you can also
> do
>
> namespace gollywogs {
> struct vector {void found_our_own() {}};
> }
> namespace PPP {
> using namespace std;
> using gollywogs::vector;
> }
>
> int main() {
> PPP::string s;
> PPP::vector v;
> v.found_our_own();
> }
>
> and that works fine. All current answers require a namespace to be
> used, and you can't get rid of it
> with a using-directive, because that stops this technique from working.
> _______________________________________________
> Ext mailing list
> Ext_at_[hidden]
> Subscription: https://lists.isocpp.org/mailman/listinfo.cgi/ext
> Link to this post: http://lists.isocpp.org/ext/2023/04/21143.php
>

Received on 2023-04-28 22:23:17