使用整数作为模板参数时的编译器错误 [英] Compiler error when using integer as template parameter

查看:144
本文介绍了使用整数作为模板参数时的编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码有什么问题?

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;
}

内部函数 B :: bar 编译器提示:


错误:类型
''和'int'到二进制运算符< ;'

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

如果A不是模板,那么一切都会很好。

If A is not a template, everything compiles fine.

推荐答案

更改 return v.foo< 13>(); 到 return v.template foo< 13& (); ,因为 foo 是一个依赖名称,你需要提到明确使用 .template 构建体。

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.

这篇关于使用整数作为模板参数时的编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆