std::construct_at 在使用 placement new 时通过指向 volatile 的指针进行转换的目的是什么?

What is the purpose for std::construct_at to cast through a pointer to volatile when using placement new?

根据cppreferencestd::construct_at(T*p, Args&&... args)等同于

return ::new (const_cast<void*>(static_cast<const volatile void*>(p)))
    T(std::forward<Args>(args)...);

演员 'through' const volatile void* 的 need/purpose 是什么?换句话说,为什么 construct_at 不等于

return ::new (static_cast<void*>(p))
    T(std::forward<Args>(args)...);

后一种代码在什么情况下会导致不良行为?

std::construct_at 接受 T 任何类型...可能有 cv-qualifiers.

同时,static_cast may not cast away constness。您建议的版本将因

而失败
const foo * ptr = get_mem();
ptr = std::construct_at(ptr); // Error here when naively static casting to void*

但是static_cast可以添加cv-qualifiers。所以为了通用,标准化的版本就避免了这个问题。它静态转换为 void 指针的最 cv-qualified 版本,然后删除那些限定符。