【发布时间】:2012-01-03 20:19:20
【问题描述】:
我有以下一组课程(我真实情况的最小复制):
namespace Parent
{
class A {};
namespace Nested
{
class A {};
}
template <typename T>
class B
{
A myA;
};
}
我希望成员Parent::B::myA 应该被明确地解析为Parent::A 类型。但是,在我的项目的其他地方,我有这个:
namespace Parent
{
using namespace Nested;
void foo()
{
B<int> myB;
}
}
在 MSVC 2003 下编译失败:
error C2872: 'A' : ambiguous symbol
could be 'Test.cpp(5) : Parent::A'
or 'Test.cpp(9) : Parent::Nested::A'
Test.cpp(26) : see reference to class template instantiation 'Parent::B<T>' being compiled
with [ T=int ]
如果我在B::myA 的声明中明确说明,代码将编译,即Parent::A myA;。但是,代码在gcc-4.3.4 下编译。这仅仅是 MSVC 2003 的一个错误,还是我真的应该担心我的模板可能被实例化的范围?
【问题讨论】:
-
这很有趣。显然,命名空间 usings 在实例化模板时按词法工作。太令人惊讶了
-
FWIW:g++ 4.5.3 同意这不应该是一个问题
-
@sehe:这不是问题(标准方面),但 MSVC 在模板方面不符合标准...(即使在最新版本中也不符合)
标签: c++ templates visual-c++ name-lookup