lundi 8 juillet 2019

std::allocator_traits::construct with const pointer

The following code compiles fine:

#include <memory>
int main()
{

    const int * a = new int; // const here!
    // *a = 5; // Compiler error.
    using at = std::allocator_traits<typename std::allocator<int>>;
    auto alloc = std::allocator<int>();
    at::construct(alloc, a);
}

Under the hood libstdc++ does

::new((void*)a) int;

but a is const!

Is this undefined behavior? Or does placement new not count as modifying?

Aucun commentaire:

Enregistrer un commentaire