88 lines
9.3 KiB
HTML
88 lines
9.3 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
|
|
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<title>C++ 具名要求:可平凡复制 (TriviallyCopyable)</title>
|
|
<link rel="stylesheet" href="ext.css" />
|
|
<link rel="stylesheet" href="site_modules.css" />
|
|
</head>
|
|
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_named_req_TriviallyCopyable skin-cppreference2 action-view cpp-navbar">
|
|
<div id="cpp-content-base">
|
|
<div id="content"><a id="top"></a>
|
|
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">C++ 具名要求:</span><span style="font-style:italic">可平凡复制</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(TriviallyCopyable)</span></h1>
|
|
<div id="bodyContent">
|
|
<div id="contentSub"><span class="subpages">< <a href="cpp.html">cpp</a>‎ | <a href="cpp-named_req.html">named req</a></span></div>
|
|
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
|
|
<p>注意:标准中并没有定义具有这个名字的具名要求。这是核心语言所定义的一种类型类别。将它作为具名要求包含于此只是为了保持一致性。</p>
|
|
<h3><span class="mw-headline" id=".E8.A6.81.E6.B1.82">要求</span></h3>
|
|
<p>下列类型统称为<i>可平凡复制类型</i>:</p>
|
|
<ul>
|
|
<li>标量类型</li>
|
|
<li>可平凡复制类类型,即满足下列条件的类类型:
|
|
<ul>
|
|
<li>至少一个复制构造函数、移动构造函数、复制赋值运算符或移动赋值运算符是合格的</li>
|
|
<li>每个<a href="cpp-language-copy_constructor.html#.E5.90.88.E6.A0.BC.E7.9A.84.E5.A4.8D.E5.88.B6.E6.9E.84.E9.80.A0.E5.87.BD.E6.95.B0">合格</a>的复制构造函数(若存在)均为<a href="cpp-language-copy_constructor.html#.E5.B9.B3.E5.87.A1.E5.A4.8D.E5.88.B6.E6.9E.84.E9.80.A0.E5.87.BD.E6.95.B0">平凡</a>的</li>
|
|
<li>每个<a href="cpp-language-move_constructor.html#.E5.90.88.E6.A0.BC.E7.9A.84.E7.A7.BB.E5.8A.A8.E6.9E.84.E9.80.A0.E5.87.BD.E6.95.B0">合格</a>的移动构造函数(若存在)均为<a href="cpp-language-move_constructor.html#.E5.B9.B3.E5.87.A1.E7.A7.BB.E5.8A.A8.E6.9E.84.E9.80.A0.E5.87.BD.E6.95.B0">平凡</a>的</li>
|
|
<li>每个<a href="cpp-language-copy_assignment.html#.E5.90.88.E6.A0.BC.E7.9A.84.E5.A4.8D.E5.88.B6.E8.B5.8B.E5.80.BC.E8.BF.90.E7.AE.97.E7.AC.A6">合格</a>的复制赋值运算符(若存在)均为<a href="cpp-language-copy_assignment.html#.E5.B9.B3.E5.87.A1.E5.A4.8D.E5.88.B6.E8.B5.8B.E5.80.BC.E8.BF.90.E7.AE.97.E7.AC.A6">平凡</a>的</li>
|
|
<li>每个<a href="cpp-language-move_assignment.html#.E5.90.88.E6.A0.BC.E7.9A.84.E7.A7.BB.E5.8A.A8.E8.B5.8B.E5.80.BC.E8.BF.90.E7.AE.97.E7.AC.A6">合格</a>的移动赋值运算符(若存在)均为<a href="cpp-language-move_assignment.html#.E5.B9.B3.E5.87.A1.E7.A7.BB.E5.8A.A8.E8.B5.8B.E5.80.BC.E8.BF.90.E7.AE.97.E7.AC.A6">平凡</a>的</li>
|
|
<li>拥有一个<a href="cpp-language-destructor.html#.E5.B9.B3.E5.87.A1.E6.9E.90.E6.9E.84.E5.87.BD.E6.95.B0">平凡</a>而未弃置的析构函数</li>
|
|
</ul>
|
|
</li>
|
|
<li><span style="font-style:italic">可平凡复制</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(TriviallyCopyable)</span> 对象的数组</li>
|
|
</ul>
|
|
<p>这意味着可平凡复制类没有<a href="cpp-language-virtual.html">虚函数</a>或<a href="cpp-language-derived_class.html#.E8.99.9A.E5.9F.BA.E7.B1.BB">虚基类</a>。</p>
|
|
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">注解</span></h3>
|
|
<table class="t-rev-begin">
|
|
<tr class="t-rev t-until-cxx20">
|
|
<td>
|
|
<p>一个复制构造函数、移动构造函数、复制赋值运算符或移动赋值运算符是合格的当且仅当它不是弃置的。</p>
|
|
</td>
|
|
<td><span class="t-mark-rev t-until-cxx20">(C++20 前)</span></td>
|
|
</tr>
|
|
</table>
|
|
<p>一般来说,对于任何可平凡复制类型 <code>T</code> 及 <code>T</code> 对象 <code>obj1</code> ,能复制 <code>obj1</code> 的底层字节到 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">char</span></span></span> 或 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">unsigned</span> <span class="kw4">char</span></span></span> <span class="t-rev-inl t-since-cxx17"><span>或 <a href="cpp-types-byte.html"><tt>std::byte</tt></a></span> <span><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></span></span> 的数组中,或到 <code>T</code> 的另一不同对象 <code>obj2</code> 中。 <code>obj1</code> 与 <code>obj2</code> 均不可为潜在重叠的子对象。</p>
|
|
<p>若复制 <code>obj1</code> 的底层字节到这种数组中,然后复制结果内容回 <code>obj1</code> 中,则 <code>obj1</code> 将保有其原值。若复制 <code>obj1</code> 的底层字节到 <code>obj2</code> 中,则 <code>obj2</code> 将保有 <code>obj1</code> 的值。</p>
|
|
<p>底层字节能由 <span class="t-lc"><a href="cpp-string-byte-memcpy.html">std::memcpy</a></span> 或 <span class="t-lc"><a href="cpp-string-byte-memmove.html">std::memmove</a></span> 复制,只要不访问存活的 volatile 对象。</p>
|
|
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
|
|
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。</p>
|
|
<table class="dsctable" style="font-size:0.8em">
|
|
<tr>
|
|
<th>DR</th>
|
|
<th>应用于</th>
|
|
<th>出版时的行为</th>
|
|
<th>正确行为</th>
|
|
</tr>
|
|
<tr>
|
|
<td><a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue1734">CWG 1734</a></td>
|
|
<td>C++11</td>
|
|
<td>拥有弃置的非平凡赋值的 C++03 POD 不是平凡的</td>
|
|
<td>允许弃置的构造函数/运算符</td>
|
|
</tr>
|
|
<tr>
|
|
<td><a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue2094">CWG 2094</a></td>
|
|
<td>C++11</td>
|
|
<td>volatile 标量类型非可平凡复制( CWG 1746 )</td>
|
|
<td>使之为可平凡复制</td>
|
|
</tr>
|
|
</table>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
|
|
<table class="t-dsc-begin">
|
|
<tr class="t-dsc">
|
|
<td>
|
|
<div class="t-dsc-member-div">
|
|
<div><a href="cpp-types-is_trivially_copyable.html"><span class="t-lines"><span>is_trivially_copyable</span></span></a></div>
|
|
<div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div>
|
|
</div>
|
|
</td>
|
|
<td>检查类型是否可平凡复制<br />
|
|
<span class="t-mark">(类模板)</span></td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
<div class="visualClear"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|