最近在我公司,我们遇到一个错误,我无法理解为什么它实际上是一个错误。在我们看来,这似乎应该可以正常编译,并允许我们显式实例化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>之前已经包含了ojit_code
namespace std {
   using ::remove;
}

关于这里发生的事情有什么想法吗?

最佳答案

看起来这与an ancient bug in gcc有关,您无法使用ns::func显式实例化模板,唯一的方法是使用namespace ns { ... func; }。这是最近才修复的,并使用newer gcc your code will compile

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

07-24 09:46
查看更多