我所试图做的是有一个可变大小的POD为平普尔在我的图书馆类:C++ 11从私有嵌套类型继承是否合法?
// header file
class foo {
public:
// ctors, copy, move, dtor, etc.
private:
struct impl; // forward-declared
impl* pimpl; // pointer to private implementation
};
然后这样定义几个固定大小的实现:
// .cpp implementation file
struct foo::impl {
uint32_t refs;
uint32_t size;
uint32_t len;
uint32_t data;
};
static_assert(sizeof(typename foo::impl) == 16, "paranoia");
namespace { // anonymous
typedef typename foo::impl base;
template <size_t S>
struct block : base {
static_assert(S > 16, "invalid block size");
static_assert(((S - 1) & S) == 0, "block size must be power of 2");
uint8_t pad[S - 16];
};
typedef block<64> block64;
typedef block<128> block128;
// ...
}
// foo implementation using the above PODs
版本的GCC 4.6和4.7没有问题用-std=c++0x -Wall -pedantic
编译,但我仍然对使用私有嵌套类型名称的合法性感到模糊。通过我的[也许过时的草案] C++ 11标准副本涉水没有给我更好的线索。
如果任何人都可以指出我的任何东西(最好是标准中的一节),证明这种方式或其他(合法与否),我会永远感激。
这看起来不合法。 “impl”是私人的,为什么任何人都可以访问它? –