C++ Logo

std-proposals

Advanced search

Re: std::as

From: Magnus Fromreide <magfr_at_[hidden]>
Date: Tue, 27 Oct 2020 10:58:22 +0100
On Mon, Oct 26, 2020 at 03:03:21PM +0100, Dusan Jovanovic (DBJ) via Std-Proposals wrote:
> ```cpp
> namespace std {
>
> // unconditionally trigger undefined behavior
> // (c) 2020 by Justin Basset
> [[noreturn]]
> inline void unreachable() {}
>
> } // std
> ```

I think this definition of unreachable() is utterly wrong.

[[noreturn]] means that the method never returns, not that it never gets
called. A canonical example of a noreturn function is std::exit.

An eventual unreachable() should instead have the property that if there
is any path in the program that leads to it then the the program is invalid,
diagnostic required.

/MF

>
> SF +1
>
>
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> Virus-free.
> www.avast.com
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> On Mon, 26 Oct 2020 at 06:26, Justin Bassett via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
> > Technically, yes, *get_if is enough, but most compilers can't recognize it
> > yet. I think gcc can under specific circumstances, but the rest I've tested
> > failed to do so.
> >
> > On Sun, Oct 25, 2020 at 6:25 PM Tony V E <tvaneerd_at_[hidden]> wrote:
> >
> >> I don't think you need unreachable.
> >> *get_if should be enough. Unconditionally dereference the result.
> >>
> >> Sent from my BlackBerry portable Babbage Device
> >> *From: *Justin Bassett via Std-Proposals
> >> *Sent: *Sunday, October 25, 2020 9:16 PM
> >> *To: *Std-Proposals
> >> *Reply To: *std-proposals_at_[hidden]
> >> *Cc: *Justin Bassett
> >> *Subject: *Re: [std-proposals] std::as
> >>
> >> I'm glad to see that clang can also optimize this get_if access; it used
> >> to be unable to do so. MSVC is also unable to optimize this when using
> >> __assume (
> >> https://docs.microsoft.com/en-us/cpp/intrinsics/assume?view=vs-2019 )
> >> (which is the same as Clang's __builtin_assume()
> >> https://clang.llvm.org/docs/LanguageExtensions.html#builtin-assume )
> >> instead of __builtin_unreachable().
> >>
> >> In theory, a portable __builtin_unreachable() would be:
> >>
> >> [[noreturn]]
> >> inline void unreachable() {} // basically, unconditionally trigger
> >> undefined behavior
> >>
> >> In practice, only GCC seems to recognize this.
> >>
> >> --Justin Bassett
> >>
> >> On Sun, Oct 25, 2020 at 5:59 PM Ryan P. Nicholl via Std-Proposals <
> >> std-proposals_at_[hidden]> wrote:
> >>
> >>>
> >>> [build] C:\Users\Ryan\rpnx-core\private\sources\all\test3.cpp(119,5): error C3861: '__builtin_unreachable': identifier not found [C:\Users\Ryan\rpnx-core\build\rpnx-core-test3.vcxproj]
> >>>
> >>> Great idea, except that __builtin_unreachable() is a GCC specific
> >>> extension, and is not part of standard C++. (It would be nice to be able to
> >>> do this in a cross platform way though! But that is for another discussion.)
> >>>
> >>> --
> >>> Ryan P. Nicholl
> >>> Tel: (678)-358-7765
> >>> Personal Email: rnicholl_at_[hidden]
> >>> Work Email: ryan.nicholl_at_[hidden]
> >>> Tox:
> >>> 52B3A61CDAF88BCC854F568933F071E3530600CDBA3D5354AADC9AD179575D68021AE959719D
> >>>
> >>>
> >>> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> >>> On Sunday, October 25, 2020 8:50 PM, Barry Revzin <
> >>> barry.revzin_at_[hidden]> wrote:
> >>>
> >>>
> >>>
> >>> On Sun, Oct 25, 2020 at 7:41 PM Ryan P. Nicholl via Std-Proposals <
> >>> std-proposals_at_[hidden]> wrote:
> >>>
> >>>> I decided to implement a new class based on std::variant. I call it
> >>>> "rpnx::derivator", but it's basically "allocating_variant". I tried to make
> >>>> it as similar to std::variant as possible. When looking at this, I noticed
> >>>> something weird about std::variant. There is no "zero overhead" way to get
> >>>> the element contained by the variant, as std::get<I> checks for invalid
> >>>> access and throws an exception if invalid. To solve this issue, I would
> >>>> like to propose std::as, which works the same as std::get, but accessing
> >>>> the wrong type is undefined behavior instead of throwing an exception.
> >>>>
> >>>> --
> >>>> Ryan P. Nicholl
> >>>> Tel: (678)-358-7765
> >>>> Personal Email: rnicholl_at_[hidden]
> >>>> Work Email: ryan.nicholl_at_[hidden]
> >>>> Tox:
> >>>> 52B3A61CDAF88BCC854F568933F071E3530600CDBA3D5354AADC9AD179575D68021AE959719D
> >>>>
> >>>
> >>> You can achieve this by using std::get_if() and marking the nullptr case
> >>> as unreachable. For example:
> >>>
> >>> auto f(std::variant<int, double>& v) -> int* {
> >>> return std::get_if<int>(&v);
> >>> }
> >>>
> >>> auto g(std::variant<int, double>& v) -> int* {
> >>> auto p = std::get_if<int>(&v);
> >>> if (not p) __builtin_unreachable();
> >>> return p;
> >>> }
> >>>
> >>> On -O1, f emits a check but g does not: https://godbolt.org/z/9G9fd5.
> >>>
> >>> Barry
> >>>
> >>>
> >>> --
> >>> Std-Proposals mailing list
> >>> Std-Proposals_at_[hidden]
> >>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> >>>
> >>
> >> --
> > Std-Proposals mailing list
> > Std-Proposals_at_[hidden]
> > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> >
>
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> Virus-free.
> www.avast.com
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2020-10-27 04:58:31