Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. To singleton or not to singleton, that is the question....
Forum Updated to NodeBB v4.3 + New Features

To singleton or not to singleton, that is the question....

Scheduled Pinned Locked Moved Solved C++ Gurus
23 Posts 4 Posters 2.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JoeCFDJ JoeCFD

    @jsulm if void baz() is defined in two namespaces, Calling baz() with using "using Foo;" can cause confusing while two namespaces are present at the same place. Therefore, Foo:: prefix is preferred.

    namespace Foo1 {
    void baz() { ... }
    }
    
    namespace Foo2 {
    void baz() { ... }
    }
    
    In another class:
    
    using Foo1;
    using Foo2;
    
    void AnotheClass::testing()
    {
            baz();  // not clear
    }
    
    ===================
    
    void AnotheClass::testing()
    {
            Foo1::baz();  // clear
    }
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #21

    @JoeCFD ...which is why we don't risk using using... ;-)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SimonSchroeder
      wrote on last edited by
      #22

      I agree that in general you should avoid using (for namespaces; now also allowed as a replacement for typedef).

      There are, however, a few select places where I do use it for namespaces: 1) When using swap the preferred method is to using std::swap; (technically not a namespace) and then call swap unqualified (without a namespace, class name, etc). 2) For using string ""s and string view ""sv literals. Haven't used std::chrono much, but most likely would import the suffixes as well.

      1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @jsulm if void baz() is defined in two namespaces, Calling baz() with using "using Foo;" can cause confusing while two namespaces are present at the same place. Therefore, Foo:: prefix is preferred.

        namespace Foo1 {
        void baz() { ... }
        }
        
        namespace Foo2 {
        void baz() { ... }
        }
        
        In another class:
        
        using Foo1;
        using Foo2;
        
        void AnotheClass::testing()
        {
                baz();  // not clear
        }
        
        ===================
        
        void AnotheClass::testing()
        {
                Foo1::baz();  // clear
        }
        
        S Offline
        S Offline
        SimonSchroeder
        wrote on last edited by
        #23

        @JoeCFD said in To singleton or not to singleton, that is the question....:

        Calling baz() with using "using Foo;" can cause confusing while two namespaces are present at the same place.

        In the concrete example you have given, it would not compile because the compiler cannot resolve the overload (i.e. Foo1::baz or Foo2::baz). You'd still need to use a fully qualified name. It only gets confusing when you have Foo1::baz(int) and Foo2::baz(double). You might write baz(1); when you actually want to call Foo2::baz. The software might even do the right thing for years. Someone might just introduce Foo1::baz years down the line and suddenly the behavior changes.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved