当我为容器使用自定义分配器时,以下代码给出了预期的结果(将大小 sz 保留为全局变量)
#include <cstddef> /* size_t */
#include <new> /* Only for bad_alloc */
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
using std::endl;
std::size_t constexpr sz = 4;
template <typename T> class StaticAllocator {
protected:
static T buf[sz];
public:
typedef T value_type;
T* allocate(std::size_t const n) {
if (n > sz) throw std::bad_alloc();
return buf;
}
void deallocate(T*, std::size_t) {}
};
template<typename T> T StaticAllocator<T>::buf[sz];
int main(void) {
std::vector<char, StaticAllocator<char> > v;
v.push_back('a');
v.push_back('b');
for (auto const& i : v) cout << i << endl;
}
当我尝试使用大小作为类的模板参数时,此版本的代码会出现编译器错误
#include <cstddef> /* size_t */
#include <new> /* bad_alloc */
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
using std::endl;
template<typename T, std::size_t sz> class StaticAllocator {
protected:
static T buf[sz];
public:
typedef T value_type;
T* allocate(std::size_t const n) {
if (n > sz) throw std::bad_alloc();
return buf;
}
void deallocate(T*, std::size_t) {}
};
template<typename T, std::size_t sz> T StaticAllocator<T, sz>::buf[sz];
int main(void) {
std::vector<char, StaticAllocator<char, 4> > v;
v.push_back('a');
v.push_back('b');
for (auto const& i : v) cout << i << endl;
}
最佳答案
获取某种类型的分配器U
来自类型 T
的分配器,成员别名模板 std::allocator_traits::rebind_alloc<U>
使用[allocator.traits.types]:
Alloc::rebind<T>::other
ifAlloc::rebind<T>::other
is valid and denotes a type; otherwise,Alloc<T, Args>
ifAlloc
is a class template instantiation of the formAlloc<U, Args>
, whereArgs
is zero or more type arguments; otherwise, the instantiation ofrebind_alloc
is ill-formed.
请注意Args
是类型模板参数。在你的分配器中没有 rebind
。第一种情况Alloc<U>
使用,Args
是空的。但在第二种情况下,第二个模板参数是一个非类型参数,它无法与 Args
匹配。 .
您需要手动添加rebind
成员结构:
template<typename T, std::size_t sz>
class StaticAllocator {
// ...
template<class U>
struct rebind {
using other = StaticAllocator<U, sz>;
};
};
另请注意,您的分配器对于某些通用类型 S
已损坏。第一,buf
将默认构造 sz
进行初始化对象 S
。然后,它将被新构造的 S
覆盖。在销毁现有位置之前先将其放在同一位置。重新分配时也会发生类似的情况。这可能会导致未定义的行为。请参阅this和 this询问一些细节。
现已删除的答案中提出了以下解决方案:继承自 std::allocator<T>
:
template<typename T, std::size_t sz> class StaticAllocator :
public std::allocator<T> {
// ...
};
代码编译并运行,但是... StaticAllocator::allocate
和StaticAllocator::deallocate
不被调用(至少在 libstdc++ 中)。原因是内部std::vector
总是uses rebind
获取分配器类型:
using Tp_alloc_type =
typename gnu_cxx::alloc_traits<Alloc>::template rebind<Tp>::other;
rebind
继承自 std::allocator<T>
它返回 std::allocator
而不是StaticAllocator
。这就是为什么您仍然需要提供自己的 rebind
在StaticAllocator
.
关于C++ 自定义分配器大小参数作为模板参数会引发编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59597432/