|
Boost : |
Subject: Re: [boost] Boost.Outcome review - First questions
From: Niall Douglas (s_sourceforge_at_[hidden])
Date: 2017-05-22 19:12:11
>> The fact optional implemented .value_or() with a return by value is a
>> mistake. Expected need not repeat that mistake.
>
> I might have missed something, but if .value_or() returns a reference, won't:
>
> auto &&s = std::optional<std::string>{}.value_or("temporary"s);
> std::cout << s << std::endl;
>
> triggers undefined behavior?
>
> '"temporary"s' is destroyed right after the declaration of 's', hence 's' is
> a dangling reference.
>
> By requiring .value_or() to return by value, this should just work.
Does not the bind to a reference keep the std::string temporary object
alive?
https://godbolt.org/g/ZcLpe5 suggests that it does.
But you might be onto something. Some other combination with value_or()
might well produce a reference to a deleted temporary. That would
explain the choice of value returning semantics for value_or().
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