Something like this would be nice:
template<bool bonus = false> void MyFunction() { foo(); bar(); if (bonus) { doBonusStuff(); } foobar(); }
Call through:
MyFunction<true>(); MyFunction<false>(); MyFunction(); // Call myFunction with the false template by default
An ugly template can be avoided by adding some useful wrappers to functions:
void MyFunctionAlone() { MyFunction<false>(); } void MyFunctionBonus() { MyFunction<true>(); }
You can find some nice information about this method there . This is an "old" paper, but the technique itself remains absolutely true.
If you have access to a good C ++ 17 compiler, you can even promote this technique using constexpr if:
template <int bonus> auto MyFunction() { foo(); bar(); if constexpr (bonus == 0) { doBonusStuff1(); } else if constexpr (bonus == 1) { doBonusStuff2(); } else if constexpr (bonus == 2) { doBonusStuff3(); } else if constexpr (bonus == 3) { doBonusStuff4(); }
Gibet Apr 25 '17 at 8:35 2017-04-25 08:35
source share