c – 手动实例化模板时出错

最近在我的公司,我们遇到了一个错误,我无法理解为什么它实际上是一个错误.对我们来说,似乎应该编译好,并允许我们显式地实例化一个类型为bar :: foo的模板.

mainy.cxx

int foo(int);
namespace bar {
  template <typename T> T foo(T a,T){return a;}
}

namespace bar {
  using ::foo;
}

template int bar::foo(int,int);

int main(){
  return 0;
}

g错误

[csteifel@host:~/test]1047 $g++ mainy.cxx
mainy.cxx:10: error: 'int bar::foo(int,int)' should have been declared inside 'bar'
mainy.cxx:10: error: 'int bar::foo(int,int)' is not declared in '::'

我们已经确认,这是gcc 4.8,4.4和clang 3.7中的错误,但它似乎与Visual Studio 2015一起使用.

当我们尝试实例化std :: remove时,遇到这个问题,但是有< algorithm>包含在< cstdio>之前和< cstdio>有了它

namespace std {
   using ::remove;
}

关于这里发生什么的任何想法?

解决方法

看起来这与 an ancient bug in gcc有关,您无法通过使用ns :: func显式实例化模板,唯一的方法是使用命名空间ns {… func; }.这只是最近固定的,与 newer gcc your code will compile.

顺便说一句,与你所说的相反,你的代码compiles with clang 3.7.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...