这是我试图创建的池对象的最小工作示例(显然功能不完整 - 我只是想说明我遇到的问题)
我有一个类模板Storage
其中包含aligned_storage
:
template<typename T, std::size_t N>
struct Storage
{
std::aligned_storage_t<sizeof(T), alignof(T)> data[N];
};
然后我有一个基类模板PoolObj
它从模板参数的静态类成员中分配T
using operator new
:
template<typename T>
struct PoolObj
{
static void* operator new(std::size_t size)
{
std::cout << "new T\n";
return &T::pool.data[0];
}
static void operator delete(void* p, std::size_t size)
{
std::cout << "delete T\n";
}
};
现在我有一个继承自的类PoolObj
,并且有一个静态Storage
member pool
,这样当我使用创建实例时new
,我将从池中获取存储空间。
struct Foo : PoolObj<Foo>
{
static Storage<Foo, 10> pool;
};
Storage<Foo, 10> Foo::pool {};
这一切都很好:
int main()
{
Foo* f = new Foo();
delete f;
return 0;
}
$ ./a.out
new T
delete T
然而,现在我正在尝试做一个PoolObj
已启用类模板:
template<typename T>
struct Bar : PoolObj<Bar<T>>
{
static Storage<Bar<T>, 10> pool;
};
template<typename T>
Storage<Bar<T>, 10> Bar<T>::pool {};
这不起作用
int main()
{
Bar<int>* b = new Bar<int>();
delete b;
return 0;
}
尝试编译时出现以下错误:
In instantiation of ‘struct Storage<Bar<int>, 10ul>’:
required from ‘struct Bar<int>’
error: invalid application of ‘sizeof’ to incomplete type ‘Bar<int>’
std::aligned_storage_t<sizeof(T), alignof(T)> data[N];
- Why is
T
in Storage
完成为Foo
,但不完整Bar<int>
et al?
- 在这里可以实现我所希望的设计吗?
完整示例如下: (以及科利鲁)
#include <type_traits>
#include <cstddef>
template<typename T, std::size_t N>
struct Storage
{
std::aligned_storage_t<sizeof(T), alignof(T)> data[N];
};
template<typename T>
struct PoolObj
{
static void* operator new(std::size_t size)
{
return &T::pool.data[0];
}
static void operator delete(void* p, std::size_t size)
{
}
};
struct Foo : PoolObj<Foo>
{
static Storage<Foo, 10> pool;
};
Storage<Foo, 10> Foo::pool {};
template<typename T>
struct Bar : PoolObj<Bar<T>>
{
static Storage<Bar<T>, 10> pool;
};
template<typename T>
Storage<Bar<T>, 10> Bar<T>::pool {};
int main()
{
Foo* f = new Foo();
delete f;
Bar<int>* b = new Bar<int>();
delete b;
return 0;
}
Edit:
有趣的是这在 clang (coliru) 中工作得很好.
第二次编辑:
根据评论,它在 VS2017 中也能工作。因此,我想我倾向于 gcc 中的错误?