Skip to content

Instantly share code, notes, and snippets.

@ldionne
Created March 3, 2017 19:28
Show Gist options
  • Save ldionne/11b81a23e063398de5c3fe535912443e to your computer and use it in GitHub Desktop.
Save ldionne/11b81a23e063398de5c3fe535912443e to your computer and use it in GitHub Desktop.
Scratchpad for compile-time control flow
void foo() {
for (auto x : tuple) {
if (runtime-bool) // ill formed
constexpr break;
}
}
void foo() {
for (auto x : tuple) {
if constexpr (compile-time-bool) // yes!
constexpr break;
}
}
void foo() {
for (auto x : tuple) {
constexpr break; // always breaks at the first iteration of the loop
}
}
void foo() {
for (auto x : tuple) {
xxx;
if constexpr (compile-time-bool)
constexpr break;
yyy; // not instantiated when the `constexpr break` is hit
}
}
void foo() {
for (auto x : tuple) {
xxx;
if constexpr (compile-time-bool)
return z;
yyy; // not instantiated when the `return` is hit
}
}
template<typename T>
void f() {
if constexpr (compile-time-bool)
constexpr return;
xxx; // never instantiated if the `constexpr return` is hit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment