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 起) |
目錄 |
[編輯] 參數
first, last | - | 要複製的元素範圍的迭代器對 |
d_first | - | 目標範圍的起始 |
policy | - | 所用的執行策略 |
類型要求 | ||
-InputIt 必須滿足老式輸入迭代器 (LegacyInputIterator) 。
| ||
-ForwardIt 必須滿足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-NoThrowForwardIt 必須滿足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-通過 NoThrowForwardIt 合法實例的自增、賦值、比較或間接均不可拋異常。將 &* 應用到 NoThrowForwardIt 值的情況下必須產生指向它的值類型的指針。(C++11 前)
|
[編輯] 返回值
指向最後複製的元素後一元素的迭代器。
[編輯] 複雜度
與 first 和 last 間的距離成線性。
[編輯] 異常
擁有名為 ExecutionPolicy
的模板形參的重載按下列方式報告錯誤:
- 如果作為演算法一部分調用的函數的執行拋出異常,且
ExecutionPolicy
是標準策略之一,那麼調用 std::terminate。對於任何其他ExecutionPolicy
,行為由實現定義。 - 如果演算法無法分配內存,那麼拋出 std::bad_alloc。
[編輯] 註解
功能特性測試宏 | 值 | 標準 | 功能特性 |
---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 的 特化的內存演算法, (1) |
[編輯] 可能的實現
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; } } |
[編輯] 示例
運行此代碼
#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_
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 出版時的行為 | 正確行為 |
---|---|---|---|
LWG 866 | C++98 | 給定 T 為 NoThrowForwardIt 的值類型,如果存在 T::operator new,那麼程序可能會非良構 |
改用全局的布置 new |
LWG 2133 | C++98 | 效果描述用到了迭代表達式是 ++d_first, ++first 的 for 循環,該表達式會進行 operator, 的實參依賴查找 |
丟棄其中一個操作數的值 以禁用該實參依賴查找 |
LWG 2433 | C++11 | 此演算法可能被重載的 operator& 劫持 | 使用 std::addressof |
LWG 3870 | C++20 | 此演算法可以在 const 存儲上創建對象 | 保持不允許 |
[編輯] 參閱
(C++11) |
複製若干對象到未初始化內存 (函數模板) |
(C++20) |
複製範圍中對象到未初始化內存 (演算法函數對象) |