I want to write some threading wrapper and I need to work with thread parameters as void * , since I am using a non-C++11 thread library. I stumbled upon a problem and prepared a minimal working example. Here is the code:
#include <iostream>
namespace chops {
template <typename T, typename U>
struct is_same {
static const bool value = false;
};
template<typename T>
struct is_same<T, T> {
static const bool value = true;
};
}
template <typename Functor, typename T>
typename Functor::return_type fun_wrapper(T const& arg) {
Functor f;
typename Functor::return_type ret = f(arg);
return ret;
}
struct hello_world {
void operator()(int count) {
while(count--)
std::cout << "hello, world!\n";
}
typedef void return_type;
};
struct is_even {
bool operator()(int x) {
if(!(x % 2))
return true;
else
return false;
}
typedef bool return_type;
};
int main() {
//fun_wrapper<hello_world>(3);
fun_wrapper<is_even>(3);
}
Here, if you want to execute the commented out line in the main, it won't compile since it wants to instantiate a template containing a line like
void x = //something
So I wrote myself an is_same type trait and want to execute that code only if Functor::return_type is not void. I want to have the effect:
if constexpr(!is_same<Functor::return_type, void>::value) {
typename Functor::return_type res = f(arg);
return ret;
} else {
f(arg);
}
Since I can't use modern C++, yet alone C++17, I can't seem to find a way. I don't know how to achieve similar effects with things like SFINAE.
PS: Note that this is a contrived example and can be made to work with some modifications but in my actual program, I need to create an object of Functor::return_type if it is not void. So please do not eliminate that line in your answers. So, no just using return f(arg).