forked from peter-can-write/cpp-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstexpr.html
More file actions
114 lines (114 loc) · 22.4 KB
/
constexpr.html
File metadata and controls
114 lines (114 loc) · 22.4 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?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="449.1854858398438"/><meta name="author" content="petergoldsborough@hotmail.com"/><meta name="created" content="2015-07-06 11:54:43 +0000"/><meta name="latitude" content="46.61396906003369"/><meta name="longitude" content="14.26306682816757"/><meta name="source" content="desktop.mac"/><meta name="updated" content="2015-08-12 10:08:27 +0000"/><title>constexpr</title></head><body>
<div>A constexpr declaration for a variable or function indicates that the compiler should evaluate the expression at compile time. For this, it must follow certain rules:</div>
<div><br/></div>
<div>“<span style="color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); float: none;">The </span><code style="font-family: 'DejaVu Sans Mono', courier, monospace !important; background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">constexpr</code><span style="color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); float: none;"> specifier declares that it is possible to evaluate the value of the function or variable at compile time. Such variables and functions can then be used where only compile time </span><a title="cpp/language/constant expression" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">constant expressions</a><span style="color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); float: none;"> are allowed (provided that appropriate function arguments are given). A </span><span style="background-color: rgba(0, 0, 0, 0.027451); border: 1px solid rgb(214, 214, 214); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 0px 2px; color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-family: 'DejaVu Sans Mono', courier, monospace !important; white-space: nowrap;"><span style="color: rgb(0, 0, 255);">constexpr</span></span></span><span style="color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); float: none;"> specifier used in an object declaration implies </span><span style="background-color: rgba(0, 0, 0, 0.027451); border: 1px solid rgb(214, 214, 214); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 0px 2px; color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-family: 'DejaVu Sans Mono', courier, monospace !important; white-space: nowrap;"><span style="color: rgb(0, 0, 255);">const</span></span></span><span style="color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); float: none;">. A </span><span style="background-color: rgba(0, 0, 0, 0.027451); border: 1px solid rgb(214, 214, 214); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 0px 2px; color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-family: 'DejaVu Sans Mono', courier, monospace !important; white-space: nowrap;"><span style="color: rgb(0, 0, 255);">constexpr</span></span></span><span style="color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); float: none;"> specifier used in an function declaration implies </span><span style="background-color: rgba(0, 0, 0, 0.027451); border: 1px solid rgb(214, 214, 214); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 0px 2px; color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-family: 'DejaVu Sans Mono', courier, monospace !important; white-space: nowrap;"><span style="color: rgb(0, 0, 221);">inline</span></span></span><span style="color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); float: none;">.</span>"</div>
<div><br/></div>
<div style="color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">
<div>A <b style="font-weight: bold;"><span style="background-color: rgba(0, 0, 0, 0.027451); border: 1px solid rgb(214, 214, 214); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 0px 2px;"><span style="font-family: 'DejaVu Sans Mono', courier, monospace !important; white-space: nowrap;"><span style="color: rgb(0, 0, 255);">constexpr</span></span></span> variable</b> must satisfy the following requirements:</div>
<div><br/></div>
</div>
<div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto;">
<ul style="list-style-type: square; padding: 0px;">
<li><font face="DejaVu Sans, arial, sans-serif">its type must be a </font><a title="cpp/concept/LiteralType" style="font-family: 'DejaVu Sans', arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; color: rgb(11, 0, 128); background-image: none;"><code style="font-family: 'DejaVu Sans Mono', courier, monospace !important; background-color: transparent;">LiteralType</code></a><font face="DejaVu Sans, arial, sans-serif">. (A “literal” is a constant value)</font></li>
<li style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-family: 'DejaVu Sans', arial, sans-serif;">it must be immediately constructed or assigned a value.</span></li>
<li style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-family: 'DejaVu Sans', arial, sans-serif;">the constructor parameters or the value to be assigned must contain only literal values, </span><code style="font-family: 'DejaVu Sans Mono', courier, monospace !important; background-color: transparent;">constexpr</code><span style="font-family: 'DejaVu Sans', arial, sans-serif;"> variables and functions.</span></li>
<li style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-family: 'DejaVu Sans', arial, sans-serif;">the constructor used to construct the object (either implicit or explicit) must satisfy the requirements of </span><code style="font-family: 'DejaVu Sans Mono', courier, monospace !important; background-color: transparent;">constexpr</code><span style="font-family: 'DejaVu Sans', arial, sans-serif;"> constructor. In the case of explicit constructor, it must have <span style="background-color: rgba(0, 0, 0, 0.027451); border: 1px solid rgb(214, 214, 214); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 0px 2px;"><span style="font-family: 'DejaVu Sans Mono', courier, monospace !important; white-space: nowrap;"><span style="color: rgb(0, 0, 255);">constexpr</span></span></span> specified.</span></li>
</ul>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-family: 'DejaVu Sans', arial, sans-serif;"><br/></span></div>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></div>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; orphans: auto; text-align: start; text-indent: 0px; widows: auto;">
<div>A <b style="font-weight: bold;"><span style="background-color: rgba(0, 0, 0, 0.027451); border: 1px solid rgb(214, 214, 214); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 0px 2px;"><span style="font-family: 'DejaVu Sans Mono', courier, monospace !important; white-space: nowrap;"><span style="color: rgb(0, 0, 255);">constexpr</span></span></span> function</b> must satisfy the following requirements:</div>
</div>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; orphans: auto; text-align: start; text-indent: 0px; widows: auto;">
<ul style="list-style-type: square; padding: 0px; list-style-image: url(data:image/png;">
<li>it must not be <a title="cpp/language/virtual" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">virtual</a></li>
<li>its return type must be <a title="cpp/concept/LiteralType" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;"><code style="font-family: 'DejaVu Sans Mono', courier, monospace !important; background-color: transparent;">LiteralType</code></a></li>
<li>each of its parameters must be literal type</li>
<li>there exists at least one argument value such that an invocation of the function could be an evaluated subexpression of a <a title="cpp/language/constant expression" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">core constant expression</a> (for constexpr function templates, at least one specialization must satisfy this requirement)</li>
</ul>
</div>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">
<table style="font-size: 13px; background-color: transparent; border-spacing: 0px; border-collapse: collapse; color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">
<tbody>
<tr>
<td style="padding: 0.3em 0.2em; border-top-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: solid none solid solid; border-top-color: silver; border-bottom-color: silver; border-left-color: silver;">
<dl style="">
<dd>
<ul style="list-style-type: square; padding: 0px; list-style-image: url(data:image/png;">
<li>the function body must be either deleted or defaulted or contain only the following:</li>
</ul>
<dl style="">
<dd>
<ul style="list-style-type: square; padding: 0px; list-style-image: url(data:image/png;">
<li>null statements</li>
<li><a title="cpp/language/static assert" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">static_assert</a> declarations</li>
<li><a title="cpp/language/typedef" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">typedef</a> declarations and <a title="cpp/language/type alias" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">alias</a> declarations that do not define classes or enumerations</li>
<li><a title="cpp/language/namespace" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">using declarations</a></li>
<li><a title="cpp/language/namespace" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">using directives</a></li>
<li>exactly one <a title="cpp/language/return" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">return</a> statement.</li>
</ul>
</dd>
</dl>
</dd>
</dl>
</td>
<td style="padding: 0.3em 0.2em; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-style: solid solid solid none; border-top-color: silver; border-right-color: silver; border-bottom-color: silver; white-space: nowrap;"><span style="color: rgb(0, 128, 0); font-size: 0.8em;">(until C++14)</span></td>
</tr>
<tr>
<td style="padding: 0.3em 0.2em; border-top-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-style: solid none solid solid; border-top-color: silver; border-bottom-color: silver; border-left-color: silver;">
<dl style="">
<dd>
<ul style="list-style-type: square; padding: 0px; list-style-image: url(data:image/png;">
<li>the function body must be either deleted or defaulted or contain <b style="font-weight: bold;">any statements except</b>:</li>
</ul>
<dl style="">
<dd>
<ul style="list-style-type: square; padding: 0px; list-style-image: url(data:image/png;">
<li>an <a title="cpp/language/asm" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">asm declaration</a></li>
<li>a <a title="cpp/language/goto" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">goto</a> statement</li>
<li>a <a title="cpp/language/try catch" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">try-block</a></li>
<li>a definition of a variable of non-literal type</li>
<li>a definition of a variable of static or thread <a title="cpp/language/storage duration" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">storage duration</a></li>
<li>a definition of a variable for which <a title="cpp/language/default initialization" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">no initialization is performed</a>.</li>
</ul>
</dd>
</dl>
</dd>
</dl>
</td>
</tr>
</tbody>
</table>
</div>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></div>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">The return types of a constexpr function can only be a LiteralType, which includes:</div>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></div>
<ul style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">
<li>void</li>
<li>scalar values (primitive types, i.e. integers, chars, etc.)</li>
<li>references</li>
<li>An array of literal type</li>
<li>Class type if:
<ul>
<li>it has a trivial (non-virtual) destructor</li>
<li>has at least one constexpr constructor</li>
<li>all of its data members are non-volatile</li>
</ul>
</li>
</ul>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></div>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">
<p style="color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">A <b style="font-weight: bold;"><span style="background-color: rgba(0, 0, 0, 0.027451); border: 1px solid rgb(214, 214, 214); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 0px 2px;"><span style="font-family: 'DejaVu Sans Mono', courier, monospace !important; white-space: nowrap;"><span style="color: rgb(0, 0, 255);">constexpr</span></span></span> constructor</b> must satisfy the following requirements:</p>
</div>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(0, 0, 0); font-family: 'DejaVu Sans', arial, sans-serif; font-size: 13px; orphans: auto; text-align: start; text-indent: 0px; widows: auto;">
<ul style="list-style-type: square; padding: 0px; list-style-image: url(data:image/png;">
<li>each of its parameters must be literal type</li>
<li>the class must have no virtual base classes</li>
<li>the constructor must not have a <a title="cpp/language/function-try-block" style="text-decoration: none; color: rgb(11, 0, 128); background-image: none; background-position: initial initial; background-repeat: initial initial;">function-try-block</a></li>
</ul>
</div>
<div style="font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style="font-family: 'DejaVu Sans', arial, sans-serif;"><br/></span></div>
</div>
</body></html>