【发布时间】:2015-03-29 05:20:51
【问题描述】:
考虑以下代码:
#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;
}
此代码编译成功并打印42 和clang 并在gcc 上给出编译时错误
'int Base::data() const [with T = Params]' 不可访问
在这种情况下,哪种实现更符合 C++ 标准?
【问题讨论】:
-
Final根本不应该看到Base,所以我认为 gcc 在这里获胜?不过,我对自己的回答持谨慎态度,因为在合规性方面我更喜欢 clang... -
@Aggieboy
Final看到Base。可访问性!= 可见性。 -
在将
using D<P>::data;添加到中间类代码后,在 g++ 上编译良好:coliru.stacked-crooked.com/a/269f221d5a8efab8 -
@alexolut 对,因为现在
data在Middle中是public,这使得Final可以访问它。 -
@UlrichEckhardt 如果您完全删除模板,clang 会给出与 g++ 相同的错误 goo.gl/aH8ZyY
error: 'Base' is a private member of 'Base'
标签: c++ inheritance g++ language-lawyer clang++