具有继承的嵌套类的名称查找

发布于 2024-11-29 19:12:32 字数 458 浏览 0 评论 0原文

这是否保证可以工作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蹲墙角沉默 2024-12-06 19:12:32

未定义的行为。

3.3.7/1

以下规则描述了类中声明的名称范围:

2) 类 S 中使用的名称 N 应在其上下文中以及在 S 的完整范围内重新评估时引用相同的声明。违反此规则不需要进行诊断。

Undefined behavior.

3.3.7/1

The following rules describe the scope of names declared in classes:

2) A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.

唔猫 2024-12-06 19:12:32

由于还没有引用,我一直在玩你的例子:

gcc 4.5.1 和 Clang 3.0 都接受代码,如下所示。

现在,我们只需要有人来挖掘出一个权威的答案。尽管 Clang、gcc 和 VC++ 都同意(不是那么频繁),但这似乎是有意为之。

ideone (4.5.1) 上:

#include <utility>

struct A
{
  struct Gold {};
};

struct B : public A
{
  typedef Gold BaseGold;
  struct Gold {};
};

struct C : public B
{
  typedef Gold BaseGold;
  struct Gold {};
};

static_assert(std::is_same<B::BaseGold, A::Gold>::value, "Not the right treasure!");
static_assert(std::is_same<C::BaseGold, B::Gold>::value, "Not the right treasure!");

Clang

#include <stdio.h>

template <typename T, typename U>
struct is_same { enum { value = false }; };

template <typename T>
struct is_same<T,T> { enum { value = true }; };

struct A
{
  struct Gold {};
};

struct B : public A
{
  typedef Gold BaseGold;
  struct Gold {};
};

struct C : public B
{
  typedef Gold BaseGold;
  struct Gold {};
};

int main() {
  if (!is_same<B::BaseGold, A::Gold>::value) {
    printf("oups");
  }
  if (!is_same<C::BaseGold, B::Gold>::value) {
    printf("oups");
  }
}

Clang 输出(如预期):

define i32 @main() nounwind readnone {
entry:
  ret i32 0
}

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):

#include <utility>

struct A
{
  struct Gold {};
};

struct B : public A
{
  typedef Gold BaseGold;
  struct Gold {};
};

struct C : public B
{
  typedef Gold BaseGold;
  struct Gold {};
};

static_assert(std::is_same<B::BaseGold, A::Gold>::value, "Not the right treasure!");
static_assert(std::is_same<C::BaseGold, B::Gold>::value, "Not the right treasure!");

On Clang:

#include <stdio.h>

template <typename T, typename U>
struct is_same { enum { value = false }; };

template <typename T>
struct is_same<T,T> { enum { value = true }; };

struct A
{
  struct Gold {};
};

struct B : public A
{
  typedef Gold BaseGold;
  struct Gold {};
};

struct C : public B
{
  typedef Gold BaseGold;
  struct Gold {};
};

int main() {
  if (!is_same<B::BaseGold, A::Gold>::value) {
    printf("oups");
  }
  if (!is_same<C::BaseGold, B::Gold>::value) {
    printf("oups");
  }
}

Clang output (as expected):

define i32 @main() nounwind readnone {
entry:
  ret i32 0
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文