Created
March 3, 2017 19:28
-
-
Save ldionne/11b81a23e063398de5c3fe535912443e to your computer and use it in GitHub Desktop.
Scratchpad for compile-time control flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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