微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

原文地址:https://www.jb51.cc/c/113925.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐