Boost logo

Boost :

Subject: Re: [boost] [outcome] expected<T&>, result<T&>
From: Niall Douglas (s_sourceforge_at_[hidden])
Date: 2017-05-25 22:59:17


>> Unless reviewers here are super keen on supporting outcome<T&>, I'll be
>> adding a static assert forever preventing them soon.
>>
>
> But not providing them means that if I have function that returns a
> reference and signals failures by, say output function parameter:
>
> ```
> T& find_smallest(std::array<T, 10> & array, std::error_code & err);
> ```
>
> I will never be able to "modernize" it to use `outcome` or `expected`.

I know you won't like this answer, but I'd just return a T* and
dereference it :)

But to be more serious, the way I've solved that exact problem in my own
code is:

```
expected<gsl::span<T>> find_smallest(std::array<T, 10> & array);
```

span's be amazing, they are effectively Rust's borrowed references. I
use them a lot.

I appreciate that is a workaround rather than a solution. But once you
start using spans a lot, you **never again** return references from a
function. References become only ever used for inputs, never outputs.
Which is a very, very good thing and your code will be much better for it.

Niall

-- 
ned Productions Limited Consulting
http://www.nedproductions.biz/ http://ie.linkedin.com/in/nialldouglas/

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