【问题标题】:C++ static template function causes armcc compilation error (304)C++静态模板函数导致armcc编译错误(304)
【发布时间】:2013-04-29 23:34:45
【问题描述】:

我已经在 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);
}

【问题讨论】:

  • 看起来像一个编译器错误。 arm-eabi-gcc 也可以正常工作。可能与 foo 在尝试编译 bar 之前不存在有关
  • 谢谢@Andres。从错误消息来看,似乎是这种情况;但是不清楚为什么bar 编译而foo 不编译。可能与所需模板的深度有关。
  • 尝试先定义 depth1(),然后定义 depth2(),或者提供 foo 的显式实例化。是的,这是一个编译器错误,我在这里提供了可能的解决方法。
  • 知道如何报告这样的错误吗?

标签: c++ templates compiler-errors


【解决方案1】:

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

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

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

  • 重新排列源文件中foobar的定义;和/或
  • 在定义之前的某处为foo 和/或bar 提供前向声明;和/或
  • 在声明后的某处添加foo&lt;int&gt; 的显式实例化,如下所示:

    template int foo(int arg);
    // or, if you like this style better,
    template int foo<int>(int arg);
    

【讨论】:

  • 你不是在模板后面缺少 吗? template&lt;&gt; int foo&lt;int&gt;(int arg);,另外,我想你也可以有template&lt;&gt; int foo(int arg);
  • @Kobi:不; template&lt;&gt; 是完全专业化的语法。 template 是显式实例化的语法,这确实是我们想要的。无耻塞:youtube.com/watch?v=vwrXHznaYLA
猜你喜欢
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
相关资源
最近更新 更多