ylliX - Online Advertising Network
Vertex AI - Antrophic and Mistral models: Why does it require Imegen access?

Using decltype(auto) for Return Type in Custom Callable Class Template


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;
};



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *