forked from peter-can-write/cpp-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstexpr.txt
More file actions
76 lines (60 loc) · 2.83 KB
/
constexpr.txt
File metadata and controls
76 lines (60 loc) · 2.83 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
# constexpr
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:
“The constexpr 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 constant
expressions are allowed (provided that appropriate function arguments
are given). A constexpr specifier used in an object declaration
implies const. A constexpr specifier used in an function declaration
implies inline."
A CONSTEXPR VARIABLE must satisfy the following requirements:
- its type must be a LiteralType. (A “literal” is a constant value)
- it must be immediately constructed or assigned a value.
- the constructor parameters or the value to be assigned must contain
only literal values, constexpr variables and functions.
- the constructor used to construct the object (either implicit
or explicit) must satisfy the requirements of constexpr constructor.
In the case of explicit constructor, it must
have constexpr specified.
A CONSTEXPR FUNCTION must satisfy the following requirements:
- it must not be virtual
- its return type must be LiteralType
- each of its parameters must be literal type
- there exists at least one argument value such that an invocation of
the function could be an evaluated subexpression of a core constant
expression (for constexpr function templates, at least one
specialization must satisfy this requirement)
- the function body must be either deleted or defaulted or contain
only the following:
- null statements
- static_assert declarations
- typedef declarations and alias declarations that do not define
classes or enumerations
- using declarations
- using directives
- exactly one return statement.
(until C++14)
- the function body must be either deleted or defaulted or contain ANY
STATEMENTS EXCEPT:
- an asm declaration
- a goto statement
- a try-block
- a definition of a variable of non-literal type
- a definition of a variable of static or thread storage duration
- a definition of a variable for which no initialization is performed.
The return types of a constexpr function can only be a LiteralType,
which includes:
- void
- scalar values (primitive types, i.e. integers, chars, etc.)
- references
- An array of literal type
- Class type if:
- it has a trivial (non-virtual) destructor
- has at least one constexpr constructor
- all of its data members are non-volatile
A CONSTEXPR CONSTRUCTOR must satisfy the following requirements:
- each of its parameters must be literal type
- the class must have no virtual base classes
- the constructor must not have a function-try-block