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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:48:28+00:00 2026-06-14T18:48:28+00:00

Declaring a class within a class is valid. (Nested classes) Declaring a namespace within

  • 0

Declaring a class within a class is valid. (Nested classes)

Declaring a namespace within a class is invalid.

The question is: is there any good reason (other than c++ grammar/syntax problems) to forbid the declaration of a namespace within a class ?


As for why would i want to do that, here is an exemple :

Let’s have a basic delcaration of a binary tree container

template<typename Data>
class binary_tree
{
 public:
  ... stuff ....     

 private:
  ... iterators class declaration ...

 public:
  typedef left_depth_iterator_impl     left_depth_iterator;
  typedef right_depth_iterator_impl    right_depth_iterator;
  typedef left_breadth_iterator_impl   left_breadth_iterator;
  typedef right_breadth_iterator_impl  right_breadth_iterator;

  ... stuff ....     

 private:
  Data         data;
  binary_tree* left;
  binary_tree* right;
};

Now i notice that there are a lot of iterators in my class, so i would like to regroup them within the same namespace like this :

template<typename Data>
class binary_tree
{
 public:
  ... stuff ....     

 private:
  ... iterators class declaration ...

 public:
  namespace iterator
  {
    typedef left_depth_iterator_impl     left_depth;
    typedef right_depth_iterator_impl    right_depth;
    typedef left_breadth_iterator_impl   left_breadth;
    typedef right_breadth_iterator_impl  right_breadth;
  }

  ... stuff ....     

 private:
  Data         data;
  binary_tree* left;
  binary_tree* right;
};

This would allow a simple usage :

void  function()
{
  binary_tree::iterator::left_depth   it;

  ...stuff...
}

This works if i use a class instead of a namespace, but i am then forced to declare a class that will never be instantiated which is quite a namespace.

Why allow nested classes and forbid nested namespaces within classes ? is it a legacy burden ?


Answers with semantic reasons that do not only quote part of the standard(especially syntax parts) will be apreciated 🙂

  • 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-14T18:48:30+00:00Added an answer on June 14, 2026 at 6:48 pm

    There’s no real advantage to adding such a feature to the language. Features generally don’t get added unless there’s demand.

    What would namespaces inside classes buy you? Would you really rather say binary_tree::iterator::left_depth instead of simply binary_tree::left_depth? Perhaps if you had multiple namespaces inside, you use them to distinguish say binary_tree::depth_iterator::left and binary_tree::breadth_iterator::right.

    In any event, you can achieve the desired result using internal classes as a poor-programmer’s namespace, which is even more reason why there isn’t demand for true namespaces inside classes.

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

Sidebar

Related Questions

What I would like to do there is declaring class variables, but actually use
I'm trying to make a good block-based initializer for an objective-c class. I'm declaring
What are the advantages-disadvantages of declaring a Delegate type within a class scope over
what could be a possible use of declaring types within a namespace but not
Declaring a class for (fast) testing purposes is great: require 'fast_helper' require 'site_search' class
hi all i am declaring id and class when creating table dynamically as below
I find myself declaring bunch of class variables and it is really tiring to
In PHP, What is the difference between declaring methods inside class like public function
I'm declaring an NSString property in a class and objective-c is complaining that: NSString
I'm declaring an instance of a class like so: Matrix m; This appears 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.