4

I'm trying to create a promise from a template function which accepts a Callable type. But I'm not sure how to do it. I tried using std::invoke_result_t, but that needs the arguments to know the result type, something which I won't know when I construct the promise.

Any way in which I can deduce the return type? I guess it may be impossible in the case of a generic lambda, but does anything work on non generic lambda cases?

template <typename Callable, typename Promise>
class Job {

  public:
    Job(Callable&& c, Promise&& p) 
    : callable(std::move(c))
    , promise(std::move(p))
    {}

    auto getFuture() {
        return /* a std::shared_future */;
    }   

    template<typename... Args>
    void call(Args&&... args) {
        promise.set_value(std::invoke(callable, args...));
    }

  private:
    Callable callable;
    Promise promise;
};

template<typename Callable>
decltype(auto) makeJob(Callable&& c) {
    std::promise</* some type */> promise = /* something */;
    return Job(std::forward<Callable>(c), std::move(promise));
}

int main() {
    f1 = [](int arg){return arg;};
    auto job = makeJob(std::move(f1));
    auto future = job.getFuture();
    job.call(1);
    std::cout << future.get() << '\n';
 }
max66
  • 65,235
  • 10
  • 71
  • 111
user3666471
  • 907
  • 11
  • 24

1 Answers1

4

Any way in which I can deduce the return type? I guess it may be impossible in the case of a generic lambda, but does anything work on non generic lambda cases?

You tagged C++17 so... std::function and deduction guides are your friends.

You can detect the return type of the callable with something as

typename decltype(std::function{c})::result_type;

so your makeJob() function can be written

template <typename Callable>
auto makeJob (Callable && c)
 {
   using retT = typename decltype(std::function{c})::result_type;

   return Job{std::forward<Callable>(c), std::promise<retT>{}};
 }

The same in the Job class.

Obviously this works because f1

auto f1 = [](int arg){return arg;};

has a return type that doesn't depend from the arguments; with a generic lambda

// ..........vvvv
auto f1 = [](auto arg){return arg;};

this solution doesn't works anymore and I don't think that you can write something to get the return type without knowing the argument type.

The following is a full compiling example

#include <future>
#include <memory>
#include <iostream>
#include <functional>

template <typename C, typename P>
class Job
 {
   public:
      using retT
         = typename decltype(std::function{std::declval<C>()})::result_type;

      Job (C && c, P && p) : callable{std::move(c)}, promise{std::move(p)}
       { }

      auto getFuture ()
       { return std::shared_future<retT>{ promise.get_future() }; }   

      template <typename ... Args>
      void call (Args && ... args)
       { promise.set_value(std::invoke(callable, args...)); }

   private:
      C callable;
      P promise;
 };

template <typename Callable>
auto makeJob (Callable && c)
 {
   using retT
      = typename decltype(std::function{c})::result_type;

   return Job{std::forward<Callable>(c), std::promise<retT>{}};
 }

int main ()
 {
   auto f1 = [](int arg){return arg;};
   auto job = makeJob(std::move(f1));
   auto future = job.getFuture();

   job.call(42);

   std::cout << future.get() << '\n';
 }
max66
  • 65,235
  • 10
  • 71
  • 111
  • 1
    That works! I did not know about decltype(std::function{c})::result_type; Seems very useful! The code compiles on g++ 8.2.0, but not clang++ 6.0.1 - tested on wandbox.org – user3666471 Aug 12 '18 at 20:18
  • Is it possible to deduce the argument types in a similar fashion? – user3666471 Aug 13 '18 at 00:16
  • @user3666471 - yes: with `std::function{c}` you deduce `std::function`, so you have also that the argument list is `ist`; but working with return type is simpler: you have the standard `result_type` type; for arguments type I suppose you have to write some custom type traits to extract and works with they. – max66 Aug 13 '18 at 11:40