std::uninitialized_copy
來自 cppreference.com
定義於標頭檔案 <memory> |
||
template< class InputIt, class NoThrowForwardIt > NoThrowForwardIt uninitialized_copy( InputIt first, InputIt last, |
(1) | (C++26 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class NoThrowForwardIt > |
(2) | (C++17 起) |
1) 將範圍
[
first,
last)
中的元素複製到從 d_first 開始的未初始化記憶體區域,如同透過:
for (; first != last; ++d_first, (void) ++first)
::new (voidify
(*d_first))
typename std::iterator_traits<NoThrowForwardIt>::value_type(*first);
如果在初始化期間丟擲異常,則已構造的物件將以未指定順序銷燬。
2) 同 (1),但根據 policy 執行。
僅當滿足以下所有條件時,此過載才參與過載決議
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 為 true。 |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 為 true。 |
(C++20 起) |
如果 d_first |
(C++20 起) |
目錄 |
[edit] 引數
first, last | - | 定義要複製的元素範圍的迭代器對 |
d_first | - | 目標範圍的開頭 |
policy | - | 要使用的 執行策略 |
型別要求 | ||
-InputIt 必須滿足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必須滿足 LegacyForwardIterator 的要求。 | ||
-NoThrowForwardIt 必須滿足 LegacyForwardIterator 的要求。 | ||
-對 NoThrowForwardIt 的有效例項,不允許遞增、賦值、比較或間接引用丟擲異常。 將 &* 應用於 NoThrowForwardIt 值必須產生一個指向其值型別的指標。(C++11 前) |
[edit] 返回值
指向最後一個被複制元素之後的元素的迭代器。
[edit] 複雜度
與 first 和 last 之間的距離呈線性關係。
[edit] 異常
帶有名為 ExecutionPolicy
的模板引數的過載會按如下方式報告錯誤:
- 如果作為演算法一部分呼叫的函式執行丟擲異常且
ExecutionPolicy
是標準策略之一,則呼叫 std::terminate。對於任何其他ExecutionPolicy
,行為由實現定義。 - 如果演算法未能分配記憶體,則丟擲 std::bad_alloc。
[edit] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 對於未初始化記憶體演算法,(1) |
[edit] 可能的實現
template<class InputIt, class NoThrowForwardIt> constexpr NoThrowForwardIt uninitialized_copy(InputIt first, InputIt last, NoThrowForwardIt d_first) { using T = typename std::iterator_traits<NoThrowForwardIt>::value_type; NoThrowForwardIt current = d_first; try { for (; first != last; ++first, (void) ++current) ::new (static_cast<void*>(std::addressof(*current))) T(*first); return current; } catch (...) { for (; d_first != current; ++d_first) d_first->~T(); throw; } } |
[edit] 示例
執行此程式碼
#include <cstdlib> #include <iostream> #include <memory> #include <string> int main() { const char *v[] = {"This", "is", "an", "example"}; auto sz = std::size(v); if (void *pbuf = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz)) { try { auto first = static_cast<std::string*>(pbuf); auto last = std::uninitialized_copy(std::begin(v), std::end(v), first); for (auto it = first; it != last; ++it) std::cout << *it << '_'; std::cout << '\n'; std::destroy(first, last); } catch (...) {} std::free(pbuf); } }
輸出
This_is_an_example_
[edit] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 866 | C++98 | 給定 T 為 NoThrowForwardIt 的值型別,如果T::operator new 存在,程式可能格式錯誤 |
使用全域性替換- new 代替 |
LWG 2133 | C++98 | 效果描述使用了一個 for 迴圈,迭代 表示式為 ++d_first, ++first,這導致 引數依賴查詢 operator, |
丟棄一個運算元的 值 以停用該 ADL |
LWG 2433 | C++11 | 此演算法可能被過載的 operator& 劫持 | 使用 std::addressof |
LWG 3870 | C++20 | 此演算法可能在 const 儲存上建立物件 | 保持不允許 |
[edit] 參閱
(C++11) |
將一定數量的物件複製到未初始化記憶體區域 (函式模板) |
(C++20) |
將物件範圍複製到未初始化記憶體區域 (演算法函式物件) |