在什么情况下新的两次返回相同的地址?

时间:2011-03-29 23:36:33

标签: c++ templates

为什么以下不能编译?它表示“错误:在'>'标记之前预期的主要表达”。

template<typename K, typename V>
struct cmpf {
const K& r;
V& visitor;

cmpf(const K& _r, V& _visitor) : r(_r), visitor(_visitor) {}

template<typename T>
void operator()(T& l) {

    if (r.type == a_type(T)) {
        return l == r.get<T>(); // DOES NOT WORK
    } else return false;
}
};

为什么编译器机器人会理解这一点? get()是在类型K中定义的带有模板参数的函数。在这种情况下,我想使用T作为参数,但编译器无法识别这一点。当我使用以下代码时,它可以工作(但它不对,因为get是一个模板函数):

    return l == r.get();

1 个答案:

答案 0 :(得分:4)

尝试

return l == r.template get<T>();

阅读此常见问题解答以获取更多信息:What is the ->template, .template and ::template syntax about?