I have a C++ piece of code
auto returnvalue = m_func(x, y, z);
where the type of m_func depends on a template parameter. Afterwards I deal with the returnvalue
which works fine until m_func
is a function returning void. But I need a mechanism which calls
m_func(x,y,z)
if the return value of m_func is void and the above version is not. Overall in pseudo code it need to look like
if ( returnvalue of m_func is void )
call m_func directly
else
auto retval = m_func(...)
handle the return value
How can these be done with C++11/14?
Edit:
m_func is either:
void func(type1 arg1, type2 arg, ...)
or
std::tuple<...> func(type1 arg1, type2 arg, ...)
Whereas C++17 has if constexpr
to handle it simply, C++11/C++14 has to use some overload to handle it via SFINAE or tag dispatching or specialization, follow up a tag dispatching version:
void internal_impl(std::true_type/*, ... */) {
m_func(x, y, z);
}
void internal_impl(std::false_type/*, ... */) {
auto value = m_func(x, y, z);
foo(value);
}
void internal(/*... */) {
internal_impl(std::integral_constant<bool,
std::is_void<decltype(m_func(x, y, z))>::value>{}
/*, ...*/);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With