【发布时间】:2015-05-23 22:41:42
【问题描述】:
以下代码在clang下不编译,但在gcc和VS下编译:
template<typename T> class bar;
namespace NS
{
template<typename T>
class foo
{
foo() {}
template<typename U> friend class bar;
};
}
template<typename R>
class bar
{
public:
bar()
{
NS::foo<int> f;
}
};
int main(int, char **)
{
bar<int> b;
return 0;
}
它失败了:
main.cpp:20:22: error: calling a private constructor of class 'NS::foo<int>'
NS::foo<int> f;
^
main.cpp:8:9: note: implicitly declared private here
foo() {}
^
bar 应该可以访问foo 的私有构造函数,但看起来它没有。如果我删除 namespace NS,它会编译。
代码对我来说看起来不错,但也许我误解了 C++ 标准。哪个编译器是正确的?
【问题讨论】:
-
旁注:如果你符合
::bar的条件,比如template<typename U> friend class ::bar;,那么clang 会编译它。所以它似乎与命名空间外的朋友可见性有关。看着一些 SO 问题,clang++ 似乎是正确的,尽管我(还)没有找到一个骗子。
标签: c++ g++ language-lawyer clang++