<分区>
使用类成员声明只允许作为成员声明的原因是什么?我做不到
struct Outer
{
typedef int Inner;
};
void f()
{
// this doesn't work with C++14
using Outer::Inner;
}
,但我认为这与使用声明将在其他地方声明的名称引入当前区域的预期语义相当正交。它受到 C++ 标准的明确限制
7.3.3/8: A using-declaration for a class member shall be a member-declaration
,但我想知道为什么。
我认为如果允许使用内部类型(typedef、类等)并定义为与
// using Outer::Inner should be identical to
using Inner = Outer::Inner
,这与命名空间的行为相同。
我知道这个问题has been asked before ,但公认的答案是禁止的,因为标准是这样说的。我想知道这背后是否有任何技术原因。在其中一条评论中,据说
Making the Standard (and therefore all compilers) more complicated to handle an unusual case that has a convenient workaround just doesn't pass the cost-benefit test.
我想说这是一种观点而不是争论。改变它使语言变得更加统一是一个巨大的好处。现在,你必须教不同的东西来做同样的事情。如果删除该子句,标准将变得更简单,这既是因为大小减小了,也是因为(无缝任意的)特殊情况被删除了。
更准确地说,我正在寻找技术原因为什么不允许这样做,例如会产生歧义或其他问题的地方。