Initializing template base-class member types in derived-class initializer lists

前端 未结 3 852
暗喜
暗喜 2020-12-25 15:18

Here is some code outlining a problem I\'ve been wrestling with. The final problem (as far as g++ is concerned at the moment) is that: \"error: \'Foo-T\' was not declared in

3条回答
  •  误落风尘
    2020-12-25 15:49

    When I first looked at your code, I was absolutely certain that you would have to resolve the problem with partial specializing. Indeed, that may still be the case, however, I have reduced the amount of code necessary to replicate your error, and have observed that the error only occurs when compiling with gcc (I don't know which compiler version my university is running though), and when compiling with Visual Studio 2003 - everythying is happy.

    The following replicates the error code, but a minor, seemingly innocent change will suprisingly eliminate it:

    template 
    class ClassA
    {
    public:
        ClassA () {}
        T vA;
    };
    
    
    template
    class ClassB : public ClassA
    {
    public:
        ClassB ()
        {
            vA = 6;
        }
    };
    
    int main ()
    {
        ClassB cb;
    }
    

    Now, if you remove the template declaration from ClassB, and have it directly inherit from ClassA:

    class ClassB : public ClassA
    {
    public:
        ClassB ()
        {
            vA = 6;
        }
    };
    

    and then change the declaration of cb to match

        ClassB cb;
    

    Then the error vanishes, even though there is clearly nothing different regarding the scope of vA (or in your case, Foo_T)

    I'm speculating that this is a compiler bug, and wondering if perhaps a completely up to date gcc compiler would still experience the issue.

提交回复
热议问题