Question

Is this code valid C++(11)?

struct Base {
    template <typename>
    struct nested;
};
struct Derived1 : Base { };
struct Derived2 : Base { };
struct Derived3 : Derived1, Derived2 { };

typedef Derived3::nested<int> xxx;

What I know

The above code fails to compile with:

  • Apple LLVM 5.0 (clang-500.2.75)
  • Clang 3.4

But it successfully compiles with:

  • gcc 4.9.0 20131110 (experimental)
  • gcc 4.8

Also, if I change the nested type to a non-template type, i.e.

struct Base {
    struct nested;
};
...
typedef Derived3::nested xxx;

then it works with the above compilers.

[edit] Changing the nested template struct to a template alias also does not change anything;

template <typename> struct dependent { struct type; };
struct Base {
    template <typename T>
    using nested = typename dependent<T>::type;
};

produces the same results with the above compilers. [end edit]

From N3242 §10.1 [class.mi]

A class can be an indirect base class more than once and can be a direct and an indirect base class. There are limited things that can be done with such a class. The non-static data members and member functions of the direct base class cannot be referred to in the scope of the derived class. However, the static members, enumerations and types can be unambiguously referred to.

I think it means that the code should be valid, but I'm not sure.

Was it helpful?

Solution

It is fine GCC is either right/more helpful (it sticks to the standard VERY strongly)

It can't see why the definition would be ambiguous because you're talking about a type not a member, and type are equal if their names are equal in C++ (names being some mangled form of types involved and such)

Addendum:

It'd be wrong if "nested" and "nested" in the other base were different. It is a struct, not a typedef or using (which are scoped)

GCC will whine if something is ambiguous, try with -pedantic if you want to make it bitch even where it is not. I see no reason why this should be rejected even if GCC is just being permissive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top