ptal / expected

What did you expect?
113 stars 18 forks source link

Possible to completely ignore error #2

Closed jhwest closed 10 years ago

jhwest commented 10 years ago
boost::expected<int> fun() {
  return boost::make_expected_from_error<int>(std::runtime_error("Test Error"));
}

int main(int argc, char* argv[]) {
  fun();
}

As the above code example runs without producing any error. This shows that it's still possible to completely ignore any error conditions. http://stackoverflow.com/questions/14923346/how-would-you-use-alexandrescus-expectedt-with-void-functions addresses this.

Destructor of expected could have an assert check that triggers if the return status has been completely ignored.

viboes commented 10 years ago

Thanks for sharing this issue.

I don't know if taking it in account only on debug mode is a solution.

std::future seems to suffer from the same issue.

jhwest commented 10 years ago

Agreed, handling in debug mode only isn't ideal. Perhaps allowing the user to specify a custom function to call (say, a logging function) would be better, but that gets more complex.

On Sun, Dec 29, 2013 at 2:39 AM, Vicente J. Botet Escriba < notifications@github.com> wrote:

Thanks for sharing this issue.

I don't know if taking it in account only on debug mode is a solution.

std::future seems to suffer from the same issue.

— Reply to this email directly or view it on GitHubhttps://github.com/ptal/Boost.Expected/issues/2#issuecomment-31314864 .

ptal commented 10 years ago

IHMO, a function returning an expected means that you retrieve an integer or the error that prevents the creation of the integer. If you don't try to retrieve this integer, then you can safely ignore the error unless the function has side effects:

expected<int> fun(int& data) { ...}

int main(int argc, char* argv[]) {
  int i = 0;
  fun(i);
  // The value of i might be something not expected (since we didn't test the return value).
}

In this last case, the designer of the function has maybe misused the expected class.

Anyways, it's not in the spirit of C++ to add a "read" boolean (as in the SO link you posted) just to be sure the user didn't ignore the value.

jhwest commented 10 years ago

Our very basic home grown version has CMExpected, with an assert check. This has come in very handy to catch programming mistakes. Personally, I would like to see this feature.

viboes commented 10 years ago

I have reached to define a ensured_read class that terminates if its underlying value has not been read. This class can be used with expected as follows

  expected<T, ensured_read<int>> e = make_unexpected(make_ensured_read(1));
  int errc = e.error(); // avoids termination.

This should not work yet for expected<T, ensured_read<exception_ptr>>.

jhwest commented 10 years ago

Great - I look forward to trying it out.

Jay

On Sun, Apr 6, 2014 at 10:12 PM, Vicente J. Botet Escriba < notifications@github.com> wrote:

I have reached to define a ensured_read class that terminates if its underlying value has not been read. This class can be used with expected as follows

expected> e = make_unexpected(make_ensured_read(1)); int errc = e.error(); // avoids termination.

This should not work yet for expected> .

Reply to this email directly or view it on GitHubhttps://github.com/ptal/Boost.Expected/issues/2#issuecomment-39696689 .

viboes commented 10 years ago

I have added an specialization of expected_traits<ensured_read<exception_ptr>>.

expected<T, ensured_read<exception_ptr>> should work now as expected ;-)