Skip to content

Instantly share code, notes, and snippets.

@NocturnDragon
Last active May 4, 2018 23:27
Show Gist options
  • Save NocturnDragon/6df914c3afacb97636809460e3edb483 to your computer and use it in GitHub Desktop.
Save NocturnDragon/6df914c3afacb97636809460e3edb483 to your computer and use it in GitHub Desktop.
Better templated functions on values syntax
// 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