考虑以下代码:
Code:
#include <iostream>
struct Params { };
template <class T>
struct Base
{
int data() const { return 42; }
};
template <template <class> class D, class P>
struct Middle : private D<P> // must be 'public' for g++
{
};
struct Final : public Middle<Base,Params>
{
using Base<Params>::data;
};
int main() {
Final f;
std::cout << f.data() << std::endl;
return 0;
}
这段代码在使用clang编译时成功并输出42
,但在gcc编译时会出现编译时错误。
'int Base::data() const [with T = Params]' 不可访问
在这种情况下,哪个实现更符合C++标准?
Final
看到Base
。可访问性 != 可见性。 - Columbousing D<P>::data;
后,代码在g++上编译良好:http://coliru.stacked-crooked.com/a/269f221d5a8efab8 - αλεχολυτMiddle
中的data
是public
的,这使得Final
可以访问它。 - Barryerror: 'Base'是'Base'的私有成员
@UlrichEckhardt - αλεχολυτ