Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6937183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:19:50+00:00 2026-05-27T12:19:50+00:00

I have a lot of namespace usage in an initialiser list and would like

  • 0

I have a lot of namespace usage in an initialiser list and would like a using namespace to reduce the verbosity. However the initialiser list is outside the scope of the constructor braces so I would have to place the using outside the constructor and pollute the rest of the file with it. Is there a way to scope the using as I want? Rather than:

MyClass::MyClass() :
    m_one(nsConstants::ONE),
    m_two(nsConstants::TWO),
    m_three(nsConstants::THREE)
{}

I want:

MyClass::MyClass() :
    using namespace nsConstants;
    m_one(ONE),
    m_two(TWO),
    m_three(THREE)
{}

_

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:19:50+00:00Added an answer on May 27, 2026 at 12:19 pm

    You can’t. The standard offers some less good alternatives:

    // The stuff you want to use.
    namespace foo { namespace bar {
        class Frob {};
    } }
    

    Now, from least polluting to most polluting.

    typedef makes it possible to write that alias in a private section of your class definition:

    // I)
    class Schwarzschild {
              typedef foo::bar::Frob FbFrob;
    public:   Schwarzschild () : a(FbFrob()), b(FbFrob()) {}
    private:  FbFrob a,b,c;
    };
    

    But you can also use it unit-globally, but with an opportunity to rename it:

    // II)
    class Schwarzschild {
    public:   Schwarzschild ();
    private:  foo::bar::Frob a,b,c;
    };
    
    // cxx-file
    typedef foo::bar::Frob FbFrob; 
    Scharzschild::Scharzschild() : a(FbFrob()) {}
    

    You can also alias namespaces:

    // III)
    namespace fb = foo::bar;
    class Planck {
    public:   Planck () : a(fb::Frob()), b(fb::Frob()) {}
    private:  fb::Frob a,b,c;
    };
    

    Or you can cherry pick symbols from other namespaces, with the disadvantage that your Frob may collide with another Frob in your unit of translation:

    //  IV)
    using foo::bar::Frob;
    class Mach {
    public:   Mach () : a(Frob()), b(Frob()) {}
    private:  Frob a,b,c;
    };
    

    Just for the sake of completeness, the most polluting solution is using namespace.

    //  V)
    using namespace foo::bar;
    class Newton {
    public:   Newton () : a(Frob()), b(Frob()) {}
    private:  Frob a,b,c;
    };
    

    Note that III, IV and V can also be limited to your cxx-file, like in the Schwarzschild-example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have lot of code in my Tornado app which looks like this: @tornado.web.asynchronous
I am creating Excel file download using NOPI libraries. I have lot of data
Have a lot of unnecessary results using contains() method in my query. Don't tell
I have a lot of pages that are setup like so from an old
So, I have a class. It's a useful class. I like a lot. Let's
this question is just speculative. I have the following implementation in C++: using namespace
Say I have a lot of c++ files that I want to add namespace
I have a lot of code like this in one of my projects (from
I have a double namespace situation, where my controllers look like this: CandidateController Candidate::PerformanceController
I would like to create a new dynamic chart control. This chart will have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.