mercredi 31 janvier 2018

Don't putting T in non-allocating new of an aligned_storage_t allocation and reinterpret_cast/deref T out of aligned_storage_t violate strict alias?

TL;DR

(1) Doesn't putting a T object in a non-allocating new of an aligned_storage_t allocation violate strict aliasing rules?

(2) Doesn't reinterpret_cast and deref T out of aligned_storage_t violate strict alias rules?


On the cppreference page of std::aligned_storage (here), there is an example code of static_vector containing a member which is an array of aligned_storage_t.

typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N];

There is an emplace_back like this:

// Create an object in aligned storage
template<typename ...Args> void emplace_back(Args&&... args) 
{
    if( m_size >= N ) // possible error handling
        throw std::bad_alloc{};
    new(data+m_size) T(std::forward<Args>(args)...);
    ++m_size;
}

Doesn't putting T in a non-allocating new of an aligned_storage_t allocation violate the strict aliasing rules?

Then there is indexer like this:

// Access an object in aligned storage
const T& operator[](std::size_t pos) const 
{
    return *reinterpret_cast<const T*>(data+pos);
}

Doesn't aliasing T out of aligned_storage_t violate the strict aliasing rules? [basic.val]/11 -- N4713 page 82 § 8.2.1 ¶ 11

Aucun commentaire:

Enregistrer un commentaire