问题描述
在类中声明类是有效的。 (嵌套类)
Declaring a class within a class is valid. (Nested classes)
在类中声明命名空间无效。
Declaring a namespace within a class is invalid.
问题是:好的原因(除了c ++语法/语法问题)禁止在类中声明命名空间?
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 historical ?
具有语义原因的答案不仅仅引用部分标准apreciated:)
Answers with semantic reasons that do not only quote part of the standard(especially syntax parts) will be apreciated :)
推荐答案
向语言添加这样的功能没有真正的优势。
There's no real advantage to adding such a feature to the language. Features generally don't get added unless there's demand.
课程中的命名空间会购买什么?你真的想说 binary_tree :: iterator :: left_depth
而不是简单的 binary_tree :: left_depth
?也许如果你在里面有多个命名空间,你可以使用它们来区分 binary_tree :: depth_iterator :: left
和 binary_tree :: breadth_iterator :: right
。
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
.
无论如何,你可以使用内部类作为穷人的命名空间来实现所需的结果,这就是为什么' t需要类中的真命名空间。
In any event, you can achieve the desired result use internal classes as a poor-man's namespace, which is even more reason why there isn't demand for true namespaces inside classes.
这篇关于为什么我们不能在类中声明一个命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!