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

  • Home
  • SEARCH
  • 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 6365271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:16:21+00:00 2026-05-25T00:16:21+00:00

I sometimes declare classes in nested namespaces and when it comes to defining their

  • 0

I sometimes declare classes in nested namespaces and when it comes to defining their member functions, I prefer not to have to qualify each one with these nested namespace names, especially if they are long-ish.

Adding “using namespace ” (or, for more precise targetting, “using ::SomeClass”) before I define the member functions seems to obviate the need to qualify each definition, but I can’t find anywhere in the spec that guarantees this, and I’m worried that it might be a behaviour that only works with GCC. I note that there doesn’t appear to be a similar mechanism for skipping the need to add the qualifiers when defining free functions(?).

As an example of what I mean:

Header:

// example.h
namespace SomeNamespace
{
    class SomeClass
    {
    public:
        void someMemberFunction();
    };

    void someFreeFunction();
};

Implementation:

// example.cpp
#include "example.h"

using namespace SomeNamespace;

void SomeClass::someMemberFunction()
{
    // OK: seems to define SomeNamespace::SomeClass::someMemberFunction(),
    // even though we didn't qualify it with SomeNamespace::
}

void someFreeFunction()
{
    // Not what we wanted; declares and defines ::someFreeFunction(), not
    // SomeNamespace::someFreeFunction() (quite understandably)
}

int main()
{
    SomeClass a;
    a.someMemberFunction(); // Ok; it is defined above.
    SomeNamespace::someFreeFunction(); // Undefined!
    return 0;
}

So my question: is the above way of definining SomeClass::someMemberFunction() legal, and where in the spec is this mentioned? If legal, is it advisable? It certainly cuts down on clutter! 🙂

Many thanks 🙂

  • 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-25T00:16:22+00:00Added an answer on May 25, 2026 at 12:16 am

    When you define a member-function, the compiler realizes that it is a member-function that must belong to a previously declared class, so it looks that class up, as specified in Section 9.3.5 of the standard:

    If the definition of a member function is lexically outside its class
    definition, the member function name shall be qualified by its class
    name using the :: operator. [Note: a name used in a member function
    definition (that is, in the parameter-declaration-clause including
    the default arguments (8.3.6), or in the member function body, or, for
    a constructor function (12.1), in a mem-initializer expression
    (12.6.2)) is looked up as described in 3.4. ] [Example:

    struct X {
        typedef int T; 
        static T count; 
        void f(T); 
    }; 
    
    void X::f(T t = count) { }
    

    The member function f of class X is defined in global scope; the
    notation X::f specifies that the function f is a member of class X and
    in the scope of class X. In the function definition, the parameter
    type T refers to the typedef member T declared in class X and the
    default argument count refers to the static data member count
    declared in class X. ]

    Basically, what you are doing is fine. However, there is another (preferable) way to cut down on the clutter when using nested namespaces, or namespaces with long names (or both) – define an alias:

    namespace short_name = averylong::nested::namespacename;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I have two modules, each containing a class, the problem is their classes
Sometimes we have several classes that have some methods with the same signature, but
Sometimes I have to work on code that moves the computer clock forward. In
i have a stupid question...sometimes i see developers use the following code... NSString *newURL
I see some examples sometimes would declare a property as well as variable other
I'm using VS2010,C# to develop my ASP.NET web app, sometimes I need to declare
I have been tasked with converting several php classes into java classes, which is
We have a log table that has a message column that sometimes has an
I have the following model classes declared by SQLAlchemy: class User(Base): id = Column(Integer,
I'm still getting confused by Objective-C. Sometimes you declare a variable like so: NSRect

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.