Is this syntax illegal?

prestokeys

The following will not compile on GCC 4.8.1:

//struct Tag {};  // Program compiles if I use this.

template <typename T>
struct Base {
    struct Tag {};
    Base(Tag) {}
};

template <typename T>
struct Derived : Base<T> {
    Derived(Tag tag) : Base<T>(tag) {}
//  Derived(Base<T>::Tag tag) : Base<T>(tag) {}
};

int main() {}

complaining [Error] expected ')' before 'tag'. It compiles on Visual Studio 2013 though, and I wanted to know if VS2013 is correct in accepting it. It does compile when I declare Tag outside of Base<T>, but I want to declare Tag inside of Base<T> where it belongs. Using Derived(Base<T>::Tag tag) : Base<T>(tag) {} didn't help either. Any way to fix the above so that both compilers accept this, while keeping Tag inside of Base<T>.

Columbo

[temp.dep]/3:

In the definition of a class or class template, if a base class depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.

Tag is used as an unqualified name - thus it can never designate the member of a depedent base class. However, Tag is also not dependent, so lookup must be resolved at definition time (prior to instantiation), which makes the program ill-formed. That can be diagnosed at either definition or instantiation time.

However, when the name is dependent (like Base<T>::Tag), name lookup is postponed and considers members of dependent base classes at instantiation time.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related