Calling a base class method in a template virtual class hierarchy

孤街浪徒 提交于 2019-11-28 04:52:20

问题


Let's say I have the following class hierarchy:

template< class T >
class TestBase {

public:

    virtual T const & do_foo() = 0;

};

template< class T >
class TestDerived : public virtual TestBase< T > {

public:

    virtual int do_bar() {
        return do_foo() + 1;
    }

};

GCC spits out the following:

error: there are no arguments to ‘do_foo’ that depend on a template parameter, so a declaration of ‘do_foo’ must be available [-fpermissive]
note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)

Now, if I change this to derive from a TestBase instantiated from a known type (e.g. class TestDerived : public virtual TestBase< int >, this snippet compiles just fine so the problem apparently has to do with the base class type being unknown at compile time. But since I haven't instantiated objects of either class anywhere, I don't see why this should even matter.

Basically, how would I resolve the error without resorting to -fpermissive?


回答1:


Non-dependent names (that is, names which do not depend on template arguments), are looked up when parsing the template, not when instantiating it. You need to make do_foo a dependent name. There are basically two ways to achieve this:

virtual int do_bar() {
    return this->do_foo() + 1;
}

or

template< class T >
class TestDerived : public virtual TestBase< T > {

public:
    using TestBase<T>::do_foo;

    virtual int do_bar() {
        return do_foo() + 1;
    }

};



回答2:


It will not win any awards for it's beauty, but this piece of code works as expected, without the compiler complaining.

virtual int do_bar() {
    return ((TestBase<T> *) this)->do_foo() + 1;
}


来源:https://stackoverflow.com/questions/14318958/calling-a-base-class-method-in-a-template-virtual-class-hierarchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!