在boost.asio的一个官方例子中,我找到了如下代码:
template <class Func, class Alloc>
void post(Func f, const Alloc& a) const
{
auto p(std::allocate_shared<item<Func>>(
typename std::allocator_traits<
Alloc>::template rebind_alloc<char>(a),
priority_, std::move(f)));
std::lock_guard<std::mutex> lock(context_.mutex_);
context_.queue_.push(p);
context_.condition_.notify_one();
}
应该rebind_alloc<char>
修改为rebind_alloc<item<Func>>
?
最佳答案
没关系。 allocate_shared
的规范说:
The
allocate_shared
templates use a copy ofa
(rebound for an unspecifiedvalue_type
) to allocate memory.
函数永远不会直接分配指针对象;它通常分配一个带有嵌入式指针的 shared_ptr 控制 block 。因此,您通过的任何分配无论如何都必须被反弹。
重新绑定(bind)到 char
的优点是它更短,并且不必针对所使用的类型进行调整。 (int
可能更短,但 char
有时被理解为“原始内存”。)程序员。
关于c++ - 这种 rebind_alloc 的使用是错误的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49093979/