Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is GCC 4.4.1 buggy by not accepting an injected-class-name in a ctor-initializer?

Tags:

c++

gcc

GCC 4.4.1 is refusing to find my injected-class-name within a ctor-initializer:

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

struct Derived : Base<int>
{
    Derived() : Base(2) {}
};

int main()
{
    Derived d;
}
test2.cpp: In constructor "Derived::Derived()":
test2.cpp:9: error: class "Derived" does not have any field named "Base"
test2.cpp:9: error: no matching function for call to "Base<int>::Base()"
test2.cpp:4: note: candidates are: Base<T>::Base(int) [with T = int]
test2.cpp:3: note:                 Base<int>::Base(const Base<int>&)

GCC 4.8 compiles it just fine, though.

I'm sure this is supposed to work, and I can't find any standard wording that disagrees with me.

This a GCC 4.4.1 bug, right?

(I did scour GCC Bugzilla but nothing relevant popped out at me.)

like image 677
Lightness Races in Orbit Avatar asked Jan 14 '14 16:01

Lightness Races in Orbit


1 Answers

Yeah, it is a bug.

I can reproduce it even more simply without the ctor-initialiser:

template <typename T>
struct Base
{
};

struct Derived : Base<int>
{
    Base* ptr;
};

int main()
{
    Derived d;
}

/**
 * in GCC 4.4.1:
 * 
 * error: ISO C++ forbids declaration of "Base" with no type
 */

And:

[C++11: 14.6.1/4]: A lookup that finds an injected-class-name (10.2) can result in an ambiguity in certain cases (for example, if it is found in more than one base class). If all of the injected-class-names that are found refer to specializations of the same class template, and if the name is used as a template-name, the reference refers to the class template itself and not a specialization thereof, and is not ambiguous. [ Example:

template <class T> struct Base { };
template <class T> struct Derived: Base<int>, Base<char> {
   typename Derived::Base b;            // error: ambiguous
   typename Derived::Base<double> d;    // OK
};

—end example ]

Notice that the near-equivalent of my unambiguous usage is "OK". Alright, so Derived is a class template here and not in my example, so it's not quite the same example. But I'm satisfied now that the entirety of 14.6.1 makes my code legal.

Turns out it had been raised as GCC bug 45515, but since it had been fixed on head at the time there are very few details on it.

Thanks BoBTFish!

like image 161
Lightness Races in Orbit Avatar answered Nov 07 '22 01:11

Lightness Races in Orbit