Error calling template method in “templated-base-class”

泄露秘密 提交于 2019-12-01 07:41:14

问题


The following code does not compile, why is that? And how can I fix this?

struct A{
    template<int N> int get() { return N; }
};

template <typename X>
struct B : public X {
    template<int N> int get() {
        return X::get<N>();
    }
};

int main(int argc, const char *argv[])
{
    B<A> b;
    return b.get<5>();
}

Compiler error:

test.cxx: In member function ‘int B<X>::get()’:
test.cxx:8:30: error: expected primary-expression before ‘)’ token
test.cxx: In member function ‘int B<X>::get() [with int N = 5, X = A]’:
test.cxx:15:25:   instantiated from here
test.cxx:8:30: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to  binary ‘operator<’

回答1:


you must disambiguate it, like so:

template<int N>int get() {
    return X::template get<N>();
}


来源:https://stackoverflow.com/questions/8755003/error-calling-template-method-in-templated-base-class

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