Last active
May 4, 2018 23:27
-
-
Save NocturnDragon/6df914c3afacb97636809460e3edb483 to your computer and use it in GitHub Desktop.
Better templated functions on values syntax
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
// I wish I could write | |
int foo(int bar, constexpr bool add) | |
{ | |
if constexpr(add) | |
{ | |
return bar + 1; | |
} | |
return 5; | |
} | |
// Instead of | |
template<bool add> int foo2(int bar) | |
{ | |
return bar; | |
} | |
template<> int foo2<true>(int bar) | |
{ | |
return bar + 1; | |
} | |
// or | |
// Instead of | |
template<bool add> int foo2(int bar) | |
{ | |
if constexpr(add) | |
{ | |
return bar + 1; | |
} | |
return bar; | |
} | |
int main() { | |
int asd; | |
// .... | |
// and call | |
return foo(asd, true); | |
// instead of | |
return foo2<true>(asd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment