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

后端 未结 2 1927
礼貌的吻别
礼貌的吻别 2021-01-12 13:56

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

template 
struct Base
{
    Base(int x) {         


        
相关标签:
2条回答
  • 2021-01-12 14:04

    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!

    0 讨论(0)
  • 2021-01-12 14:11

    Use : Base<int>(2) {}

    (edit: sorry, I just took away the CRTP element as it's not needed to reproduce)

    0 讨论(0)
提交回复
热议问题