c++ - 使用 boost void 分配器是不好的做法吗?

标签 c++ boost allocator boost-interprocess

我有时看到人们使用这样定义的通用空分配器:

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 both allocate and deallocate member functions, which cannot be synthesized by default from allocator_traits. That need went away with C++11 and the void_pointer and const_void_pointer type aliases in allocator_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/

相关文章:

c++ - 在 Visual Studio 2012 中从一个项目引用另一个项目中的 DLL

c++ - 如何将 boost 路径类型转换为字符串?

c++ - 使用自定义分配器修改增长策略

c++ - 在 vc++ 中获取事件进程名

c++ - Boost.Asio 与谷歌 Protocol Buffer

c++ - NULL 终止 c_str()?

c++ - 如何手动创建带有 XML 属性的 boost ptree?

C++ Boost Regex 不保存捕获

c++ - 用于进程间共享内存的非 Boost STL 分配器?

c++ - 使用自定义分配器使 std::list 缓存友好?