Firstly, I have already seen Constexpr if alternative, but that didn't help.
I updated the post to clarify explicitly the necessity of the generic solution.
What I need is a generic solution to use the potential of C++17 if constexpr in C++14. Maybe, we can do something with lambdas and/or boost::hana here?
Below you will find a small example of using if constexpr - I don't need the solution for the below-mentioned case only - rather a generic one, that can be applied in most cases as the replacement of if constexpr if not everywhere.
You can assume I am porting C++17 code with if constexpr to C++14 - what is the simplest way of doing that?
I am thinking of solution, which might look like below, but not sure how to implement that:
if<condition>(func1).elif<condition2>(func2).else(func3)
Other ideas are highly welcome as well.
C++17 code to test:
#include <string>
template<class T>
constexpr bool value = true;
template<>
constexpr bool value<std::string> = false;
template<class T>
void method(const T& arg) {
if constexpr (value<T>) {
if (!arg) return;
}
//mylogic(arg);
}
int main() {
std::string arg;
method(arg);
int arg2;
method(arg2);
return 0;
}
You can just do it the old fashioned way with
std::enable_ifby moving the type dependent code into a separate function with specialisations: