C++ 命名要求: CopyConstructible
來自 cppreference.com
指定該型別的例項可以從左值表示式進行復制構造。
目錄 |
[編輯] 要求
型別 T 滿足 CopyConstructible,如果:
- 型別
T滿足 MoveConstructible,並且
給定
以下表達式必須有效並具有其指定的效果
| 表示式 | 後置條件 |
|---|---|
| T u = v; | u 的值等同於 v 的值。 v 的值未改變。 |
| T(v) | T(v) 的值等同於 v 的值。 v 的值未改變。 |
|
表示式 v.~T() 也必須有效,並且,對於左值 v,表示式 &v 的型別必須是 |
(C++11 前) |
[編輯] 注意
直到 C++11,過載了 operator& 的類不是 CopyConstructible,因此不能在標準庫容器中使用。這是 C++98 中的一個設計決策(而不是缺陷,參見 LWG 問題 390)。
自 C++11 起,當需要物件的地址時,標準庫使用 std::addressof。
| 擴充套件內容 |
|---|
|
作為 CopyConstructible 類意味著 std::is_copy_constructible,但反之不然,因為 std::is_copy_constructible 只會檢查是否能夠用正確的引數呼叫建構函式,例如,不檢查 MoveConstructible 要求。 執行此程式碼 #include <type_traits> #include <utility> struct S { S() = default; S(S&&) = delete; S(const S&) = default; }; static_assert(std::is_copy_constructible_v<S>); int main() { S s1; // Class `S` doesn't satisfy MoveConstructible requirement, // hence doesn't satisfy CopyConstructible requirement [[maybe_unused]] S s2{std::move(s1)}; // ill-formed, use of deleted function } |
[編輯] 參考
| 擴充套件內容 |
|---|
|
[編輯] 另請參見
| (C++11)(C++11)(C++11) |
檢查型別是否具有複製建構函式 (類模板) |
| (C++20) |
指定型別的物件可以被複制構造和移動構造 (概念) |