使用模板编译器错误 [英] Ugly compiler errors with template

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

问题描述

template<typename T>
struct function
{
   typedef T type;
   template<typename U>
   static void f() {}
};

template<typename T>
struct caller
{
        int count;
        caller(): count() {}
        void operator()()
        {
                count++;
                T::f<typename T::type>();
        }
};

int main() {
        caller<function<int> > call;
        call();
        return 0;
}

这似乎对我来说是正确的,但编译器给出了这个丑陋的错误,了解:

This seems correct to me, but compiler gives this ugly error which I am unable to understand:


prog.cpp:在成员函数'void caller :: operator()()':

prog.cpp:17:错误:预期`('before'>'标记

prog.cpp:17:错误:预期的主表达式')'

prog.cpp: In member function ‘void caller::operator()()’:
prog.cpp:17: error: expected `(' before ‘>’ token
prog.cpp:17: error: expected primary-expression before ‘)’ token

为了您的便利,代码会发布在这里 - > http: //www.ideone.com/vtP7G

For your convinence, code is posted here -> http://www.ideone.com/vtP7G

推荐答案

T::template f<typename T::type>();

如果没有此模板,代码将解析为:

Without this "template", the code is parsed as:

T::f [less-than operator] typename T::type [greater-than operator]...

这是错误。

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

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