C++静态模板函数导致armcc编译错误(304)

lev*_*gli 5 c++ templates compiler-errors

我已经在VS10和armcc4.1 [Build 561]上测试了以下代码的编译.函数depth1()和depth2()都在VS上编译,但是armcc只会编译depth1(),同时为depth2()提供错误304(没有匹配参数列表的实例).当foo和bar是非静态的时,它也会在armcc上编译得很好.

我很乐意明白为什么.

template <class T>
static T foo(T arg)
{
   return arg*5;
}

template <class T>
static T bar(T arg)
{
   return foo<T>(arg);
}

void depth2()
{
   int i = 12;
   i = bar<int>(i);
}

void depth1()
{
   int i = 12;
   i = foo<int>(i);
}
Run Code Online (Sandbox Code Playgroud)

Quu*_*one 3

根据上面的评论:这似乎是armcc 4.1 中的一个错误。

如果您的雇主与 ARM 签订了支持合同,您可以在此处向 ARM 提出支持问题:http://www.arm.com/support/obtaining-support/index.php(单击“开发工具”选项卡,然后单击大蓝色“提出支持案例”按钮)。

至于解决方法,您可以尝试

  • 重新排列源文件中foo和的定义;bar和/或
  • 在其定义之前foo和/或某处提供前向声明;bar和/或
  • foo<int>在声明后添加某个地方的显式实例化,如下所示:

    template int foo(int arg);
    // or, if you like this style better,
    template int foo<int>(int arg);
    
    Run Code Online (Sandbox Code Playgroud)