Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't we declare a namespace within a class?
    primarykey
    data
    text
    <p>Declaring a class within a class is valid. (Nested classes)</p> <p>Declaring a namespace within a class is invalid.</p> <p>The question is: is there any good reason (other than c++ grammar/syntax problems) to forbid the declaration of a namespace within a class ?</p> <hr> <p>As for why would i want to do that, here is an exemple :</p> <p>Let's have a basic delcaration of a binary tree container</p> <pre><code>template&lt;typename Data&gt; 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; }; </code></pre> <p>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 :</p> <pre><code>template&lt;typename Data&gt; 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; }; </code></pre> <p>This would allow a simple usage :</p> <pre><code>void function() { binary_tree::iterator::left_depth it; ...stuff... } </code></pre> <p>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.</p> <p>Why allow nested classes and forbid nested namespaces within classes ? is it historical ?</p> <hr> <p>Answers with semantic reasons that do not only quote part of the standard(especially syntax parts) will be apreciated :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload