Boost logo

Boost :

Subject: Re: [boost] [outcome] How to drop the formal empty state
From: Peter Dimov (lists_at_[hidden])
Date: 2017-05-26 16:32:50


Niall Douglas wrote:
> But if I am writing my implementation that way, it is because there is no
> purpose here to my function returning empty. Else I would implement it
> differently.
>
> I appreciate that the above is me speaking vaguely from experience and
> without concrete evidence.

I usually place high value on argument from experience, even if presented
without evidence.

But in this case, you've cited

https://github.com/ned14/boost.afio/blob/master/include/boost/afio/v2.0/detail/impl/posix/file_handle.ipp#L188

which is this:

result<file_handle> file_handle::file(file_handle::path_type _path,
file_handle::mode _mode, file_handle::creation _creation,
file_handle::caching _caching, file_handle::flag flags) noexcept
{
  result<file_handle> ret(file_handle(native_handle_type(), 0, 0,
std::move(_path), _caching, flags));
  native_handle_type &nativeh = ret.get()._v;
  BOOST_OUTCOME_TRY(attribs,
attribs_from_handle_mode_caching_and_flags(nativeh, _mode, _creation,
_caching, flags));
  nativeh.behaviour |= native_handle_type::disposition::file;
  const char *path_ = ret.value()._path.c_str();
  nativeh.fd = ::open(path_, attribs, 0x1b0 /*660*/);
  if(-1 == nativeh.fd)
    return make_errored_result<file_handle>(errno,
last190(ret.value()._path.native()));
  BOOST_AFIO_LOG_FUNCTION_CALL(nativeh.fd);
  if(!(flags & flag::disable_safety_unlinks))
  {
    BOOST_OUTCOME_TRYV(ret.value()._fetch_inode());
  }
  if(_creation == creation::truncate &&
ret.value().are_safety_fsyncs_issued())
    fsync(nativeh.fd);
  return ret;
}

Now, the first line constructs

  result<file_handle> ret(file_handle(native_handle_type(), 0, 0,
std::move(_path), _caching, flags));

and it's immediately apparent that returning `ret` in this initial state
makes no sense. That is, you either need to further initialize it into a
valid file_handle, or return an "errored" result.

For this reason, I'd think that a style that does

    file_handle fh(native_handle_type(), 0, 0, std::move(_path), _caching,
flags);

first, then initializes it appropriately, and as a final step returns it via
result<file_handle>, would perhaps make more sense (and also save a number
of ret.value() calls).


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk