lamarrr/STX

Wrapped inside a promise

Invander29 opened this issue · 5 comments

Add a possibility to create std::promise/std::future with stx::Result inside.

auto p = std::promise<stx::Result<A, B>>();
p.set_value(stx::Ok(A{}));

Thank you

Hello,

Thanks for your issue.

I've tried this snippet and it works.

Can you perhaps be more detailed by providing the compiler error message, your configuration and compiler name?

I am using your 1.0.1 version and this code works for me:

auto p = std::promise<Option<int>>();
// or
auto p = std::promise<Option<std::unique_ptr<int>>>();

But Result not

auto p = std::promise<Result<int, int>>();
// or
auto p = std::promise<Result<std::shared_ptr<int>, std::shared_ptr<int>>>();

Visual Studio 2019 (16.6.3) shows:

future(212): error C2280: 'stx::v1::Result<int,int>::Result(void)': attempting to reference a deleted function
stx/internal/option_result.h(1701): message : see declaration of 'stx::v1::Result<int,int>::Result'
stx/internal/option_result.h(1701,3): message : 'stx::v1::Result<int,int>::Result(void)': function was explicitly deleted
future(209): message : while compiling class template member function 'std::_Associated_state<_Ty>::_Associated_state(std::_Deleter_base<_Ty> *)'
2>        with
2>        [
2>            _Ty=stx::v1::Result<int,int>
2>        ]
future(1150): message : see reference to function template instantiation 'std::_Associated_state<_Ty>::_Associated_state(std::_Deleter_base<_Ty> *)' being compiled
2>        with
2>        [
2>            _Ty=stx::v1::Result<int,int>
2>        ]
future(1150): message : see reference to class template instantiation 'std::_Associated_state<_Ty>' being compiled
2>        with
2>        [
2>            _Ty=stx::v1::Result<int,int>
2>        ]
future(1150): message : while compiling class template member function 'std::promise<stx::v1::Result<int,int>>::promise(void)'
cpp(83): message : see reference to function template instantiation 'std::promise<stx::v1::Result<int,int>>::promise(void)' being compiled
cpp(83): message : see reference to class template instantiation 'std::promise<stx::v1::Result<int,int>>' being compiled

The fault isn't from STX, rather from the MSVC C++ std lib implementation.
Result, unlike Option is not default-constructible and your MSVC C++ std lib implementation for promise performs a default construction step. Unlike GCC and Clang.

The code compiles on GCC and Clang.

See some similar issues:

https://developercommunity.visualstudio.com/content/problem/60897/c-shared-state-futuresstate-default-constructs-the.html

https://developercommunity.visualstudio.com/content/problem/372171/stdpromise-doesnt-support-types-that-are-not-defau.html

Ok, thank you.

Ok, thank you.

You're welcome