voidify()
is used in various constructors in specialized.algorithms that were introduced in c++20. It isn't clear to me what it's supposed to do. It's given as:
template<class T>
constexpr void* voidify(T& obj) noexcept {
return const_cast<void*>(static_cast<const volatile void*>(addressof(obj)));
}
What is the reason for casting an address into a pointer to a const volatile void
, and then back into just a pointer to a void
?
I'm guessing that it has something to do with telling the compiler's optimizer, somehow, that an object assigned to the memory it ultimately addresses should toss out all prior cache (read or writes) that it knows about. But I can't find any description anywhere of how this is supposed to work.
Note: This question is neither the same as the related, but different question here, not does that answer address this question.
That question was why one couldn't simply use static_cast<void*>
and the answer was the obviously correct one that static_cast
is not allowed to cast away const but can add cv's.