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 9161201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:54:54+00:00 2026-06-17T13:54:54+00:00

Both Class and Namespace? This question is about a pattern that I am seeing

  • 0

Both Class and Namespace?

This question is about a pattern that I am seeing myself use more and more: Having both a class and a namespace for related concepts. I think this is motivated mainly by C++ language artifacts, but not totally.

I suppose the top level question is: Is this a good idea? Having both a class and a namespace for related concepts?

Lower level question:

What is the best way to do this?

Class nested within the namespace?:

namespace Foo_Namespace {
   class Foo_Class {
       ...
   };
}

Or separate, peer, class and namespace?:

class Foo_Class {
    ...
};
namespace Foo_Namespace {
   // non-class free functions, etc.
}

I must admit that I lean towards nesting the class within the namespace.
Even though it leads to ugly names.
But even if I do that, what naming conventions should I use:

The following is too long, leading to really ugly names Foo_Namespace::Foo_Class

namespace Foo_Namespace {
   class Foo_Class {
       ...
   };
}

It isn’t necessary to use any suffixes or indicators in the name:

namespace Foo {
   class Foo {
       ...
   };
}

But then I find myself uncertain, when I look at Foo::bar(), if that is a free function bar in namespace ::Foo, i.e. ::Foo::bar(), or a member function in class Foo in namespace ::Foo::Foo::bar().

And names like ::Foo::Foo::bar are still not, umm, nice.

Currently I am doing

It isn’t necessary to use any suffixes or indicators in the name:

namespace Foo_ns {
   class Foo {
       ...
   };
}

mainly because I usually create the class first, and then later realize that a namespace would be nice.

I wonder if I should revive a naming convention I haven’t used in years: _c for class, _ns for namespaces:

namespace Foo_ns {
   class Foo_c {
       ...
   };
}

Details:

I won’t repeat what I have said above, but I’ll add a bit more.

The most practical reason I know of to use a namespace in addition to a class is that you are allowed to do forward declarations of free functions in a namespace, but you are not allowed to do forward declarations of some methods of a class. I.e. with a class you have to declare it all or nothing. Whereas with free functions in general, and free functions in namespaces in particular, you can declare it piecemeal. Parts can be declared in different header files. Rather than #including a massive header-only-library for a massive class, you can use forward declarations for just one or two functions. Etc.

(See, for example, http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Forward_Declarations, although Google netly comes down against forward declarations rather than including a header.)

Another reason is that a namespace allows the class itself to be kept small.

The biggest advantage for a class is that a class can be passed to a template, whereas a namespace cannot be.

I prefer to have the class nested within the namespace, Foo_ns/Foo_ns::Foo_c rather than to have them as peers Foo_ns/Foo_c, because often a class needs helper classes, e.g. Foo_ns/Foo_ns::Foo_c/Foo_ns::Foo_Helper_c. If the class and the namespace are peers, it seems strange to have Foo_ns/Foo_c but Foo_ns::Foo_Helper_c.

I like namespaces, because I agree with Andrei Alexandresciu: http://laser.inf.ethz.ch/2012/slides/Alexandrescu/1-C++%20course%20parts%201%20and%202.pdf

 Class methods vs. free functions

 • Conventional wisdom: methods are cool, free functions are so 1960s
 • Yet:
   ◦ Free functions improve encapsulation over methods
   ◦ Free functions may be more general
   ◦ Free functions decouple better
   ◦ Free functions support conversions on their left-hand argument

  Surprising fact #2

    Making a function a method should be
    your last, not first, choice

  (c) 2012– Andrei Alexandrescu. 32 / 59

It’s better to create free functions than methods in classes.

But sometimes classes just have to be used – e.g. for templates.

Naming convention wise

I used Foo_ns / Foo_ns::Foo_c in the past

I’m using Foo_ns / Foo_ns::Foo now

(By the way, I tend to use Class_Names_With_Underscores_and_Initial_Caps, not CamelCase.)

If it makes sense, I might elide the _ns suffix on the namespace – e.g. where the namespace and the class(es) do not need to have the same name.

I dislike making them having the same name. Consider a constructor for a class Foo inside a namespace foo:

::Foo::Foo::Foo()
vs
::Foo_ns::Foo::Foo()

The latter is not much better, but is a bit less confusing.

I think that I usually create the class on its own, without nesting it in a namespace.
In fact, I probably add a few static methods, before I realize that a class nested within a namespace would be better. By that stage it may be a pain to refactor, and I sometimes end up creating forwarding functions, that forward from the class static method to the free function in the namespace, or vice versa. This causes me to regret bot to jump to class wthin namespace right from step 1.

Conclusion

My current BKM is Foo_ns / Foo_ns::Foo, i.e.

namespace Foo_ns {
   class Foo { 
   ...
   };
}

I’d appreciate any suggestions, any improvements.

Or am I just broken for doing this?

  • 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-06-17T13:54:56+00:00Added an answer on June 17, 2026 at 1:54 pm

    I recommend having your class in a namespace with related functions. A big reason for this is argument-dependent lookup (ADL). When you call a non-member function that has a class type argument, the function name is looked up in the enclosing namespace of that class. So if you have, say:

    namespace foo {
      class bar { };
      void baz(bar);
    }
    

    If you ever want to call baz, you won’t need to explicitly give the namespace it is contained in, you can simply do:

    foo::bar x;
    baz(x);
    

    Without qualifying baz, the compiler still found the function because it is within a namespace that encloses the type of its argument. In this way, C++ considers the contents of the enclosing namespace of a class to be part of the interface of that class. Implementing functions that are part of the interface of a class as non-member non-friend functions in this way, allowing ADL to find the function, increases encapsulation. If a function doesn’t need access to the internals of a class, don’t make it a member of that class – instead, put it in the same namespace as that class.

    It’s suspicious that your namespace has the same name as your class, however. Usually there will be more than one class within a namespace, and even if there weren’t, the names would be different. The name of the namespace should describe its contents, not just the single class inside it.

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

Sidebar

Related Questions

This is related to another question of mine, and unfortunately I'm having a difficult
We have two projects that are both class libraries. Project 1 is a VS
What if I have a class that both extends an abstract class and implements
I have a parent and child class that both need to implement IDisposable .
I am having problems creating a managed class with namespace in C++/CLI. I would
Disclaimer: this question is directly related to my programming homework. My C++ assignment consists
I started to use the unordered_set class from the tr1 namespace to speed-up access
I'm trying to teach myself about OOP in C#, but I have a question
I may be going about this backwards... I have a class which is like
Apple Mail defines both a class account and a constant account for the rule

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.