I’m experimenting with decltype(auto)
in a custom callable class template that simplifies the behavior of std::function
. My goal is to maintain the exact return type, including qualifiers like constness, reference etc, of the callable object when it is invoked. Here is the very simplified version of the class.
P.S. : C++ uses R
instead of decltype(auto)
:
https://en.cppreference.com/w/cpp/utility/functional/function/operator()
I was wondering if I can use decltype(auto)
with the same effect as R
template <typename>
class Callable;
template <typename R, typename... Args>
class Callable
{
public:
template<typename F>
Callable(F&& f) : func(std::forward<F>(f)) {}
decltype(auto) operator()(Args... args) {
return func(std::forward<Args>(args)...);
}
private:
std::decay_t<R(Args...)> func;
};