【发布时间】:2021-05-07 20:04:17
【问题描述】:
我想知道为什么未初始化的存储功能像 https://en.cppreference.com/w/cpp/memory/uninitialized_copy 和 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 的情况?还是我错过了什么?
【问题讨论】: