Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name-lookup of nested classes with inheritance

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...

like image 237
ltjax Avatar asked Aug 12 '11 12:08

ltjax


1 Answers

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.

like image 121
AProgrammer Avatar answered Oct 03 '22 23:10

AProgrammer