Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cxx::optional and cxx::expected adjustments #160

Closed
elfenpiff opened this issue Jun 29, 2020 · 2 comments · Fixed by #176
Closed

cxx::optional and cxx::expected adjustments #160

elfenpiff opened this issue Jun 29, 2020 · 2 comments · Fixed by #176
Assignees
Labels
enhancement New feature refactoring Refactor code without adding features

Comments

@elfenpiff
Copy link
Contributor

elfenpiff commented Jun 29, 2020

Brief feature description

  • The methods of cxx::expected on_success and on_error should be renamed to and_then and or_else.
  • It should be possible to create an cxx::optional directly from an cxx::expected with a method called .to_optional() which returns the value of the cxx::expected packed inside an cxx:optional if it contains a value otherwise it returns the cxx::nullopt
  • If a value is moved outside the cxx::optional the method has_value should no longer return true since we would like to avoid undefined behavior
cxx::optional<int> a = 5;
if ( a.has_value() ); // returns true
int b = std::move(*a);
if ( a.has_value() ); // should now return false since the value is moved outside the optional

// this case would false assume that the optional has no more contained value but at the moment I do not see a problem with that.
std::move(*a);
  • If a value is moved outside the cxx::expected it should no longer be contained. We need the methods has_value() and has_error() which both should return false in that case. If the methods and_then() or or_else() are called on an cxx::expected in that state they should terminate.

Detailed information

We would further reduce undefined behavior.

@elfenpiff elfenpiff added enhancement New feature refactoring Refactor code without adding features labels Jun 29, 2020
@elfenpiff elfenpiff self-assigned this Jun 29, 2020
@elfenpiff
Copy link
Contributor Author

elfenpiff commented Jul 7, 2020

@elBoberido sadly it is not possible to invalidate an optional when the value is moved out since
int b = std::move(*a); calls the dereferencing operator which returns T& and the std::move is transforming it into an rvalue. This could only be done for the method T&& value() && but then it would be inconsistent in the behavior. We have the same problem also in the expected

elfenpiff added a commit to elfenpiff/iceoryx that referenced this issue Jul 7, 2020
…to and_then and on_error to or_else and deprecated the previous methods

Signed-off-by: Christian Eltzschig <[email protected]>
elfenpiff added a commit to elfenpiff/iceoryx that referenced this issue Jul 7, 2020
elfenpiff added a commit to elfenpiff/iceoryx that referenced this issue Jul 8, 2020
elfenpiff added a commit to elfenpiff/iceoryx that referenced this issue Jul 8, 2020
@dkroenke
Copy link
Member

dkroenke commented Jul 8, 2020

Would you please create a link to the according C++ standards where the adjustments are based on. That makes comparing easier.

elfenpiff added a commit to elfenpiff/iceoryx that referenced this issue Jul 8, 2020
Signed-off-by: Eltzschig Christian (CC-AD/ESW1) <[email protected]>
elfenpiff added a commit that referenced this issue Jul 9, 2020
…-adjustments

Iox #160 optional and expected adjustments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature refactoring Refactor code without adding features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants