具有继承的嵌套类的名称查找
这是否保证可以工作:
struct A
{
struct Gold {};
};
struct B : public A
{
typedef Gold BaseGold;
struct Gold {};
};
struct C : public B
{
typedef Gold BaseGold;
struct Gold {};
};
static_assert(is_same<B::BaseGold, A::Gold>::value, "Not the right treasure!");
static_assert(is_same<C::BaseGold, B::Gold>::value, "Not the right treasure!");
它似乎可以在 VS2010 上工作。显然它依赖于微妙的声明顺序/名称查找规则,所以我想知道标准在这件事上说了些什么......
Is this guaranteed to work:
struct A
{
struct Gold {};
};
struct B : public A
{
typedef Gold BaseGold;
struct Gold {};
};
struct C : public B
{
typedef Gold BaseGold;
struct Gold {};
};
static_assert(is_same<B::BaseGold, A::Gold>::value, "Not the right treasure!");
static_assert(is_same<C::BaseGold, B::Gold>::value, "Not the right treasure!");
It seems to work on VS2010. Obviously it relies on subtle declaration order/name lookup rules, so I was wondering what the standard says on the matter...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
未定义的行为。
3.3.7/1
Undefined behavior.
3.3.7/1
由于还没有引用,我一直在玩你的例子:
gcc 4.5.1 和 Clang 3.0 都接受代码,如下所示。
现在,我们只需要有人来挖掘出一个权威的答案。尽管 Clang、gcc 和 VC++ 都同意(不是那么频繁),但这似乎是有意为之。
在 ideone (4.5.1) 上:
在 Clang:
Clang 输出(如预期):
Since there's been no quote yet, I've been playing around with your example:
Both gcc 4.5.1 and Clang 3.0 accept the code as can be seen below.
Now, we just need someone to dig out an authoritative answer. With Clang, gcc and VC++ agreeing though (not that frequent), it seems intended.
On ideone (4.5.1):
On Clang:
Clang output (as expected):