【发布时间】:2014-05-02 10:50:37
【问题描述】:
警告,C++ 讨厌... 我在这里看到了这个问题略有不同的变化,这是我的看法
namespace space {
}
template <typename T> struct C
{
void foo() {
using namespace space;
bar(t);
}
T t;
};
class A {
};
namespace space {
void bar(const A& a){
}
}
int main()
{
C<A> c;
c.foo();
return 0;
}
如果 bar 不在命名空间内,则一切编译正常。放置命名空间会破坏 gcc 的编译。更有趣的是 - gcc 找到了函数,但就是不想使用它:
ConsoleApplication1.cpp: In instantiation of 'void C<T>::foo() [with T = A]':
ConsoleApplication1.cpp:26:10: required from here
ConsoleApplication1.cpp:8:8: error: 'bar' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point
instantiation [-fpermissive]
bar(t);
^
ConsoleApplication1.cpp:18:6: note: 'void space::bar(const A&)' declared here, later in the translation unit
void bar(const A& a){
除了标准这样说之外,这种行为是否有任何合理的理由?有什么开关可以让 gcc 接受这个代码,因为它似乎没有技术问题,而且我不明白为什么从纯语言的角度来看,这样的代码不应该工作。
【问题讨论】:
-
与模板参数不同,命名空间符号必须在调用时可见。因此无法编译代码。
-
@DieterLücking,但使用错误中显示的
-fpermissive开关除外。但这不是一个好主意。
标签: c++ templates gcc namespaces