From Jason Turner

[meta.const.eval]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpti1ataij/{from.md → to.md} +47 -0
tmp/tmpti1ataij/{from.md → to.md} RENAMED
@@ -26,5 +26,52 @@ constexpr void f(unsigned char *p, int n) {
26
  }
27
  ```
28
 
29
  — *end example*]
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  }
27
  ```
28
 
29
  — *end example*]
30
 
31
+ ``` cpp
32
+ consteval bool is_within_lifetime(const auto* p) noexcept;
33
+ ```
34
+
35
+ *Returns:* `true` if `p` is a pointer to an object that is within its
36
+ lifetime [[basic.life]]; otherwise, `false`.
37
+
38
+ *Remarks:* During the evaluation of an expression `E` as a core constant
39
+ expression, a call to this function is ill-formed unless `p` points to
40
+ an object that is usable in constant expressions or whose complete
41
+ object’s lifetime began within `E`.
42
+
43
+ [*Example 2*:
44
+
45
+ ``` cpp
46
+ struct OptBool {
47
+ union { bool b; char c; };
48
+
49
+ // note: this assumes common implementation properties for bool and char:
50
+ // * sizeof(bool) == sizeof(char), and
51
+ // * the value representations for true and false are distinct
52
+ // from the value representation for 2
53
+ constexpr OptBool() : c(2) { }
54
+ constexpr OptBool(bool b) : b(b) { }
55
+
56
+ constexpr auto has_value() const -> bool {
57
+ if consteval {
58
+ return std::is_within_lifetime(&b); // during constant evaluation, cannot read from c
59
+ } else {
60
+ return c != 2; // during runtime, must read from c
61
+ }
62
+ }
63
+
64
+ constexpr auto operator*() const -> const bool& {
65
+ return b;
66
+ }
67
+ };
68
+
69
+ constexpr OptBool disengaged;
70
+ constexpr OptBool engaged(true);
71
+ static_assert(!disengaged.has_value());
72
+ static_assert(engaged.has_value());
73
+ static_assert(*engaged);
74
+ ```
75
+
76
+ — *end example*]
77
+