Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `std::uninitialized_copy/move etc.` not constexpr?

I was wondering why the uninitialized storage functions like https://en.cppreference.com/w/cpp/memory/uninitialized_copy and https://en.cppreference.com/w/cpp/memory/uninitialized_move are not constexpr in C++20?

Going off of the "possible implementation" provided, wouldn't it only take converting

template<class InputIt, class ForwardIt>
ForwardIt uninitialized_copy(InputIt first, InputIt last, ForwardIt d_first)
{
    typedef typename std::iterator_traits<ForwardIt>::value_type Value;
    ForwardIt current = d_first;
    try {
        for (; first != last; ++first, (void) ++current) {
            ::new (static_cast<void*>(std::addressof(*current))) Value(*first);
        }
        return current;
    } catch (...) {
        for (; d_first != current; ++d_first) {
            d_first->~Value();
        }
        throw;
    }
}

to

template<class InputIt, class ForwardIt>
constexpr ForwardIt uninitialized_copy(InputIt first, InputIt last, ForwardIt d_first)
{
    typedef typename std::iterator_traits<ForwardIt>::value_type Value;
    ForwardIt current = d_first;
    try {
        for (; first != last; ++first, (void) ++current) {
            std::construct_at(current, *first); // <---- THIS
        }
        return current;
    } catch (...) {
        for (; d_first != current; ++d_first) {
            d_first->~Value();
        }
        throw;
    }
}

for the uninitialized_copy case? Or am I missing something?

like image 301
Yamahari Avatar asked Oct 26 '25 06:10

Yamahari


1 Answers

See P2283.

You're correct for uninitialized_copy, you just need to change the placement new to std::construct_at.

But for uninitialized_default_construct, you can't just use std::construct_at since that does value initialization and the algorithm needs to do default initialization. The paper suggests a new std::default_construct_at as the solution to that problem.

like image 183
Barry Avatar answered Oct 28 '25 23:10

Barry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!