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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:23:51+00:00 2026-06-18T08:23:51+00:00

What is the best/cleanest way to use namespaces in packaged code? E.g. in libraries

  • 0

What is the best/cleanest way to use namespaces in packaged code?

E.g. in libraries like boost there seem to be very organized namespaces management, some techniques are used that allow to disambiguate names. The important thing, however, is that one won’t see much code like

typedef namespace1::namespace2::sth_else::a_class<namespace3::namespace4::b_class> type;

usually, there’s not much cross-namespacing, which indicates good architecture but also a good namespace management. The question is: what IS the good namespace management?

Say we have file structure like this:

component1/...  (depends on reusable_if)
component2/...  (depends directly on reusable_if and on component 1)
reusable/
reusable/some_part/
reusable/some_part/...
reusable/some_other_part/
reusable/some_other_part/...
reusable/SthThatUsesBothReusableParts.h   (implements reusable_if/ISth.h)
reusable/SthThatUsesBothReusableParts.cpp (implements reusable_if/ISth.h)
reusable_if/
reusable_if/ISth.h   (pure abstract class)
reusable_if/ISthElse.h (pure abstract class)
main.cpp (e.g. instantiates SthThatUsesBothReusableParts and passes to component1/2)

The reason why there is reusable_if/ folder is because both component1 and component2 want to reuse the same interfaces (hence none of them ‘owns’ the interfaces exclusively). Also, the assumption is that the project is indeed very big and needs proper namespaces for classes in each of the folders.

How would you apply namespaces in such a project?
Say I declare all classes in reusable/ in namespace ::reusable. Should I put interfaces from reusable_if into namespace ::reusable or into ::reusable_if? Or maybe into none since it is used by component1 and component2?

What about namespaces in component1 and component2? Anything to remember?
What about keyword using? Let’s say that I decide to add this ::reusable_if namespace. Can I put using reusable_if into header files in component1 and component2, provided that using ... is placed inside namespace ::component1 and ::component2?

I am open to any suggestions, also those not necessarily related to the above example.

  • 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-18T08:23:52+00:00Added an answer on June 18, 2026 at 8:23 am

    Here’s what I use for my projects. My main rule is that each directory is a namespace and each file is a class, with very few exceptions (sometimes I group helper functions in subdirectories of a namespace detail, but without another nested namespace).

    1. Keep your entire project inside a single top-level namespace named after your project.

    2. Keep each utility component inside the top-level namespace, but in a separate directory. This is the only time I don’t let my namespaces overlap with my directory tree.

    3. Keep each independently releasable component within your project inside a nested namespace named after your component. For convenience, provide a single header named after your component and containing your entire component interface either in the directory corresponding to your namespace or directly at the top-level directory of your project.

    4. Keep the implementation of each component inside a nested namespace detail. Contrary to classes, namespace do not have language support for private members, but the convention in Boost is that namespace detail should not directly be called by user-code.

    No further nesting than project::component::detail::function() or project:::component::class.member() is required. If you provide complete interfaces that facilitate ADL, you can reuse your component functions inside your project as function(x) for a variable x of type project::component::class without worrying about name clashes.

    Note that in Uncle Bob’s language: “the unit of reuse is the unit of release“. Each component should provide a bunch of coherent and mutually dependent classes and functions. In particular, it should provide a complete interface for that component. The C++ language will support this through argument-dependent-lookup (ADL). See this old column “Namespaces and the Interface Principle” by Herb Sutter.

    The presence of reuse_if and the presence of both component and reusable might be a code smell, unless the considerations that you mentioned in the comments actually apply. A component should be the unit of release. If you could independently reuse a piece of code, make it a separate component. If the code depends on another piece, release it together with that other code. See Sutter’s column for these dependency relations.

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

Sidebar

Related Questions

What's the cleanest/best way in C# to convert something like 400AMP or 6M to
What's the best (cleanest) way to provide this sort of logic? var colors =
I was looking for the best/cleanest way to iterate over a list of strings
What's the cleanest way to add a prefix to every URL in CakePHP, like
I'm wondering what's the best, the cleanest and the most simply way to work
I am looking to determine the cleanest / best practice way to complete the
I was wondering what do you think would be the best and cleanest way
Casting an Iterator<Object> to a Set<String> What would be the cleanest/best practice way?
When using the Ti.UI.iPad.SplitWindow what is the best(cleanest looking) way to update the detailView
If I have a URl like /api/User/Details/2c021192-25cb-43e1-9bba-3bd5604a0a3d what would be the best way to

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.