danvratil / qcoro

C++ Coroutines for Qt
https://qcoro.dvratil.cz
MIT License
326 stars 53 forks source link

waitFor(QCoro::Task<T>): Do not require T to be default constructible #222

Closed joesdiner closed 1 month ago

joesdiner commented 1 month ago

If you use QCoro::waitFor with a QCoro::Task<T>, the type T must be default constructible. It would nice to be able to use this function with types that do not have a default constructor.

T is required to be default constructible because in impl/waitfor.h:76 a default T is constructed, whose value is immediately overwritten in the call to QCoro::details::runCoroutine().

Example compile error from gcc with T being a boost::outcome::result<int, std::string>`:

In file included from ../../../deps/include/qcoro5/qcoro/qcorotask.h:568,
                 from ../../../deps/include/qcoro5/QCoro/QCoroTask:1,
                 from ../../../test.h:15,
../../../deps/include/qcoro5/qcoro/impl/waitfor.h: In instantiation of 'T QCoro::detail::waitFor(Awaitable&&) [with T = boost::outcome_v2::basic_result<int>, std::__cxx11::basic_string<char>, boost::outcome_v2::policy::exception_ptr_rethrow<int>, std::__cxx11::basic_string<char>, void> >; Awaitable = QCoro::Task<boost::outcome_v2::basic_result<int>, std::__cxx11::basic_string<char>, boost::outcome_v2::policy::exception_ptr_rethrow<int>, std::__cxx11::basic_string<char>, void> > >]':
../../../deps/include/qcoro5/qcoro/impl/waitfor.h:97:30:   required from 'T QCoro::waitFor(QCoro::Task<T>&&) [with T = boost::outcome_v2::basic_result<int>, std::__cxx11::basic_string<char>, boost::outcome_v2::policy::exception_ptr_rethrow<int>, std::__cxx11::basic_string<char>, void> >]'
RemoteCollectorDatabase.h:27:30:   required from here
../../../deps/include/qcoro5/qcoro/impl/waitfor.h:76:11: error: use of deleted function 'boost::outcome_v2::basic_result<R, S, NoValuePolicy>::basic_result() [with R = int>; S = std::__cxx11::basic_string<char>; NoValuePolicy = boost::outcome_v2::policy::exception_ptr_rethrow<int>, std::__cxx11::basic_string<char>, void>]'
   76 |         T result;
      |           ^~~~~~
In file included from ../../../deps/include/boost/outcome/std_result.hpp:34,
                 from ../../../deps/include/boost/outcome/boost_result.hpp:60,
                 from ../../../deps/include/boost/outcome/result.hpp:34,
                 from ../../../libsceptre/test.h:14,
../../../deps/include/boost/outcome/basic_result.hpp:372:3: note: declared here
  372 |   basic_result() = delete;