What does the mean in [dcl.constexpr] / 3 mean
In [dcl.constexpr] / 3 ( http://eel.is/c++draft/dcl.constexpr#3 ), what does it mean to contain in "or a compound statement that does not contain "?
For example:
constexpr int f(bool b) {
return b ? ([]() { goto x; x: return 1; })() : 2;
}
int main() {}
Doesn't the compound statement that contains the body f
: s contain a goto statement?
I am not getting an error from Clang or GCC for this example.
+3
source to share
1 answer
If you try to use this function in context constexpr
, you get the corresponding error:
constexpr int f(bool b) {
return b ? ([]() { goto x; x: return 1; })() : 2;
}
int main() {
static_assert(f(true)==2, "");
}
non-literal type 'const (lambda at main.cpp: 2: 18)' cannot be used in constant expression
Eliminating the lambda, but keeping the goto will also give an error:
constexpr int g() { goto x; x : return 1; }
constexpr int f(bool b) {
return b ? g() : 2;
}
int main() {
static_assert(f(true)==2, "");
}
main.cpp:1:21: error: statement not allowed in constexpr function
constexpr int g() { goto x; x : return 1; }
^
0
source to share