Trying to do something like...
template <class T>
struct Wrapper
{
template <class U>
void set(const U& u) { myT = u; }
template <class F>
void set(F f) { myT = f(); }
T myT;
};
I know I need to use SFINAE here but how do I distinguish a callback parameter from a value parameter? It's safe to assume that a value can't be used as a callback.
I've tried enable_if
with is_function, result_of
, invoke_result
, is_invocable
, and others, but none of it works right. Is it even possible?
You may overload a function template either by a non-template function or by another function template. The function call f(1, 2) could match the argument types of both the template function and the non-template function.
Operator Overloading in C++ In C++, we can make operators work for user-defined classes. This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading.
What is the difference between function overloading and templates? Both function overloading and templates are examples of polymorphism features of OOP. Function overloading is used when multiple functions do quite similar (not identical) operations, templates are used when multiple functions do identical operations.
Function overloading is used when multiple functions do similar operations; templates are used when multiple functions do identical operations. Templates provide an advantage when you want to perform the same action on types that can be different.
You can do it without SFINAE:
template<class U>
void set(const U& u) {
if constexpr (std::is_invocable_v<U>)
myT = u();
else
myT = u;
}
or in a more generic way:
template<class U>
void set(U&& u) {
if constexpr (std::is_invocable_v<U>)
myT = std::forward<U>(u)();
else
myT = std::forward<U>(u);
}
Yes you can apply SFINAE with the help of std::is_invocable
(since C++17).
template <class U>
std::enable_if_t<!std::is_invocable_v<U>> set(const U& u) { myT = u; }
template <class F>
std::enable_if_t<std::is_invocable_v<F>> set(F f) { myT = f(); }
LIVE
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