Skip to content

Commit 3d9d38a

Browse files
committed
[dcl.constexpr] modernize example of constexpr-usable functions
1 parent a272b7c commit 3d9d38a

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

source/declarations.tex

+16-23
Original file line numberDiff line numberDiff line change
@@ -879,33 +879,26 @@
879879

880880
\begin{example}
881881
\begin{codeblock}
882-
constexpr int square(int x)
883-
{ return x * x; } // OK
884-
constexpr long long_max()
885-
{ return 2147483647; } // OK
886-
constexpr int abs(int x) {
887-
if (x < 0)
888-
x = -x;
889-
return x; // OK
882+
constexpr int square(int x) { // OK
883+
return x * x;
890884
}
891-
constexpr int constant_non_42(int n) { // OK
892-
if (n == 42) {
893-
static int value = n;
894-
return value;
895-
}
896-
return n;
885+
constexpr std::generator<int> iota(int n = 0) {
886+
while (true) co_yield n++; // error: \tcode{co_yield} cannot be used in a constexpr function
897887
}
898-
constexpr int uninit() {
899-
struct { int a; } s;
900-
return s.a; // error: uninitialized read of \tcode{s.a}
888+
constexpr int stinit(int n) { // \tcode{stinit} is constexpr-usable, but \tcode{stinit}
889+
static int value = n; // may not be called in a constant expression
890+
return value;
901891
}
902-
constexpr int prev(int x)
903-
{ return --x; } // OK
904-
constexpr int g(int x, int n) { // OK
905-
int r = 1;
906-
while (--n > 0) r *= x;
907-
return r;
892+
constexpr int uninit() { // \tcode{uninit} is constexpr-usable, but a program which calls
893+
struct { int a; } s; // \tcode{uninit} in a constant expression is ill-formed
894+
return s.a;
908895
}
896+
struct Base {
897+
constexpr Derived() = default; // OK
898+
};
899+
struct Derived : virtual Base {
900+
constexpr Derived() = default; // error: constructor cannot be constexpr in a
901+
}; // class or struct with virtual base classes
909902
\end{codeblock}
910903
\end{example}
911904

0 commit comments

Comments
 (0)