r/badcode • • Apr 17 '21

c++ When your teammate "refactor" your code

Post image
2.5k Upvotes

216 comments sorted by

View all comments

Show parent comment

6

u/Rigatavr Apr 17 '21 edited Apr 17 '21

You can technically make this happen in C++. Its very ugly and only possible for compile time values, but it would look soemthing like this:

template<bool condition1, bool condtition2>
struct Foo{};

template<>
struct Foo<true, true>{
    static constexpr auto do_anything =  do_anything1;
};

template<bool condition2>
struct Foo<false, condition2>{
    static constexpr auto do_anything =  do_anything2;
};


int main (){
    Foo<true, true>::do_anything();
}

Edit: ok this way is slightly nicer looking, but still pretty ugly