forked from peter-can-write/cpp-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.html
More file actions
33 lines (33 loc) · 8.74 KB
/
delete.html
File metadata and controls
33 lines (33 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta name="exporter-version" content="Evernote Mac 6.3 (452849)"/><meta name="altitude" content="519.0022583007812"/><meta name="author" content="petergoldsborough@hotmail.com"/><meta name="created" content="2015-08-01 21:20:38 +0000"/><meta name="latitude" content="46.62867006417612"/><meta name="longitude" content="13.82439488710994"/><meta name="source" content="desktop.mac"/><meta name="updated" content="2015-08-01 21:46:01 +0000"/><title>delete</title></head><body>
<div>The special ‘= delete’ syntax, when used after a function declaration, declares that function cannot be used. This is especially (or only) useful to delete implicitly/compiler generated functions such as the copy-constructor and copy-assignment operator. Note that only the specific overload where '= delete' is declared is actually deleted, other overloads without the '= delete' are left untouched. Moreover, the '= delete' must be specified (or not) the first time the function is declared (i.e. in the header, mostly) and cannot be added later.</div>
<div><span style="font-size: 11px;"><span style="font-family: Menlo;"><span style="color: rgb(228, 72, 171);"><br/></span></span></span></div>
<div><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">class</span> <span style="font-family: Menlo;"><span style="font-size: 11px;">Uncopyable</span></span></div>
<div><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">{<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">public</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">:<br/>
<br/>
Uncopyable(</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">int</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">x =</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #f32300">0</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">)<br/>
: _x(x)<br/>
{ }<br/>
<br/>
Uncopyable(</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">const</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #4f8187">Uncopyable</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">& other) =</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">delete</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">;<br/>
<br/>
Uncopyable(</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #4f8187">Uncopyable</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">&& other);<br/>
<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #4f8187">Uncopyable</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">&</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">operator</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">=(</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">const</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #4f8187">Uncopyable</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">& a) =</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">delete</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">;<br/>
<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">void</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">foo() =</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">delete</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">;<br/>
<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">void</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">foo(</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">int</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">x) { _x = x; }<br/>
<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">private</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">:<br/>
<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">int</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">_x;<br/>
};<br/>
<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">int</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">main(</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">int</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">argc,</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">char</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">* argv[])<br/>
{<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #4f8187">Uncopyable</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">a(</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #f32300">5</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">);</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #848784">// OK, not a deleted overload of Uncopyable()</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures"><br/>
<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #4f8187">Uncopyable</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">b(a);</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #848784">// Error: Call to deleted constructor of Uncopyable</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures"><br/>
<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #4f8187">Uncopyable</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">c(</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #02a1f3">std</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">::</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #3d1d81">move</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">(a));</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #848784">// OK, not a deleted overload of Uncopyable()</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures"><br/>
<br/>
c = a;</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #848784">// Error: Overload resolution selected deleted operator '='</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures"><br/>
<br/>
c.</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #31595d">foo</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">(</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #f32300">5</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">);</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #848784">// OK, not a deleted overload of foo()</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures"><br/>
<br/>
c.foo();</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #848784">// Error: Attempt to use a deleted function</span></div>
<div><span style="font-size: 11px;"><span style="font-family: Menlo;">}</span></span></div>
</body></html>