我有时看到人们使用这样定义的通用空分配器:
using namespace boost::interprocess;
typedef allocator<void, managed_shared_memory::segment_manager> VoidAllocator;
这被认为是一种不好的做法吗?我发现 std::allocator 已贬值,我应该担心 boost 版本吗?
最佳答案
是的,原因是std::allocator<void>
现在不受欢迎的也适用于其他声称是“虚空分配者”的事物。
根据p0174r0
Similarly,
std::allocator<void>
is defined so that various template rebinding tricks could work in the original C++98 library, but it is not an actual allocator, as it lacks bothallocate
anddeallocate
member functions, which cannot be synthesized by default fromallocator_traits
. That need went away with C++11 and thevoid_pointer
andconst_void_pointer
type aliases inallocator_traits
. However, we continue to specify it in order to avoid breaking old code that has not yet been upgraded to support generic allocators, per C++11.
您可以轻松定义模板别名。
using namespace boost::interprocess;
template <typename T>
using segment_allocator = allocator<T, managed_shared_memory::segment_manager>;
关于c++ - 使用 boost void 分配器是不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51627263/