I'm trying to provide a get_deleter()
for my lock-free allocator class template (code is here). The deleter is something like
template <typename T>
struct deleter {
allocator<T>& alloc;
void operator()(T* p) const noexcept {
p->~T();
alloc.deallocate(p);
}
};
Note that alloc
should not be const
, since deallocate()
is not const
, which aligns with std::allocator::deallocate()
. Now, I'm not sure whether my allocator::get_deleter()
should be const
. The dilemma is as follows:
1. Rationale for being const
: The method itself doesn't modify *this
, and is thread-safe (also see Does const mean thread-safe in C++11?).
2. Rationale for not being const
: The method returns a deleter
that can be used to modify *this
. Also avoids const_cast()
that is necessary if the method is const
.
Any suggestion or idea? Personally, I'm in favor of const
.