为什么 std::uninitialized_copy/move 等不是 constexpr?
Why is `std::uninitialized_copy/move etc.` not constexpr?
我想知道为什么未初始化的存储功能像
https://en.cppreference.com/w/cpp/memory/uninitialized_copy and https://en.cppreference.com/w/cpp/memory/uninitialized_move 不是 C++20 中的 constexpr?
离开提供的“可能的实施”,难道只需要转换
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;
}
}
到
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;
}
}
对于 uninitialized_copy
案例?还是我遗漏了什么?
见P2283。
你对 uninitialized_copy
是正确的,你只需要将 placement new 更改为 std::construct_at
。
但对于 uninitialized_default_construct
,您不能只使用 std::construct_at
,因为那样会进行值初始化并且算法需要进行默认初始化。该论文提出了一个新的 std::default_construct_at
作为该问题的解决方案。
我想知道为什么未初始化的存储功能像 https://en.cppreference.com/w/cpp/memory/uninitialized_copy and https://en.cppreference.com/w/cpp/memory/uninitialized_move 不是 C++20 中的 constexpr?
离开提供的“可能的实施”,难道只需要转换
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;
}
}
到
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;
}
}
对于 uninitialized_copy
案例?还是我遗漏了什么?
见P2283。
你对 uninitialized_copy
是正确的,你只需要将 placement new 更改为 std::construct_at
。
但对于 uninitialized_default_construct
,您不能只使用 std::construct_at
,因为那样会进行值初始化并且算法需要进行默认初始化。该论文提出了一个新的 std::default_construct_at
作为该问题的解决方案。