我试图为我的无锁分配程序类模板提供一个get_deleter() (代码是这里)。删除的内容类似于
template <typename T>
struct deleter {
allocator<T>& alloc;
void operator()(T* p) const noexcept {
p->~T();
alloc.deallocate(p);
}
};注意,alloc不应该是const,因为deallocate()不是const,它与std::allocator::deallocate()是一致的。现在,我不确定我的allocator::get_deleter()是否应该是const。目前的困境如下:
方法本身不修改“`this`”,并且是线程安全的(也请参阅const**:* )。
方法不为const**:* `this. Also avoidsconst_cast()that is necessary if the method isconst`.的理由是返回一个可用于修改`this. Also avoidsconst_cast()that is necessary if the method isconst`.的deleter。
有什么建议或想法吗?就我个人而言,我支持const。
发布于 2018-03-13 12:39:59
不要const_cast<allocator &>(*this)
您不知道是否有人会有一个const allocator<...>值并得到未定义的行为。即使你不这么做,你也在对const的真实情况撒谎。
allocator::get_deleter不可能是没有const_cast的const,所以它不应该。
看它的实况
https://stackoverflow.com/questions/49254880
复制相似问题