加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:42:38 所属栏目:百科 来源:网络整理
导读:最近在我的公司,我们遇到了一个错误,我无法理解为什么它实际上是一个错误.对我们来说,似乎应该编译好,并允许我们显式地实例化一个类型为bar :: foo的模板. mainy.cxx int foo(int);namespace bar { template typename T T foo(T a,T){return a;}}namespace b
最近在我的公司,我们遇到了一个错误,我无法理解为什么它实际上是一个错误.对我们来说,似乎应该编译好,并允许我们显式地实例化一个类型为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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读