C++ Logo

std-proposals

Advanced search

Re: Fwd: Middle ground between "return" and exceptions?

From: Dmitry Dmitry <dimanne_at_[hidden]>
Date: Mon, 14 Sep 2020 22:47:07 +0100
Yes, the trick with coroutines looks interesting and promising.


> Bary's proposal I will study carefully, but here is mine completely
> different one
>
> template<typename T> struct valstat final {
> T* value;
> std::errc status;
> };
>
> valstat<const char> FindUsersCity() noexcept
> {
> if (auto [contacts, errc] = GetOrOpenContactsServerConnection(); contacts)
> {
> if (auto [uid, errc] = contacts->GetUserId(); uid) {
> if (auto [geo, errc] = GetOrOpenGeoServerConnection(); geo) {
> if (auto [uloc, errc] = geo->GetLocation(uid); uloc) {
> return uloc->GetCityName();
> }
> else {
> return { {}, errc };
> }
> }
> else {
> return { {}, errc };
> }
> }
> else {
> return { {}, errc };
> }
> }
> else {
> return { {}, errc };
> }
> }
>
>
To be perfectly honest, it is opposite to the early-return pattern: 1
<https://softwareengineering.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement>,
2 <https://arne-mertz.de/2016/12/early-return/> :)

-- 
Dmitry
*Sent from gmail*

Received on 2020-09-14 16:47:37