Compiler error when using integer as template parameter

走远了吗. 提交于 2019-12-07 02:53:23

问题


What is wrong with the following piece of code?

template<typename X>
struct A {
        template<int N>
        int foo() const {
                return N;
        }
};

template<typename X>
struct B {
        int bar(const A<X>& v) {
                return v.foo<13>();
        }
};

#include <iostream>
using std::cout;
using std::endl;

int main() {
        A<double> a;
        B<double> b;
        cout << b.bar(a) << endl;
        return 0;
}

Inside the function B::bar the compiler complains:

error: invalid operands of types ‘’ and ‘int’ to binary ‘operator<’

If A is not a template, everything compiles fine.


回答1:


Change return v.foo<13>(); to return v.template foo<13>(); because foo is a dependent name and you need to mention that explicitly using .template construct.



来源:https://stackoverflow.com/questions/3691420/compiler-error-when-using-integer-as-template-parameter

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