【问题标题】:Ugly compiler errors with template带有模板的丑陋的编译器错误
【发布时间】:2026-01-24 08:20:03
【问题描述】:
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;
}

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

prog.cpp:在成员函数'void caller::operator()()'中:
prog.cpp:17:错误:预期 `(' 在 '>' 标记之前
prog.cpp:17: 错误: ')' 标记之前的预期主表达式

为了方便起见,代码贴在这里 -> http://www.ideone.com/vtP7G

【问题讨论】:

    标签: c++ templates


    【解决方案1】:
    T::template f<typename T::type>();
    

    没有这个“模板”,代码被解析为:

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

    这是一个错误。

    【讨论】: