[PATCH] Implement std::visit<R> for C++2a (P0655R1)

Ville Voutilainen ville.voutilainen@gmail.com
Fri Apr 5 23:19:00 GMT 2019


On Sat, 6 Apr 2019 at 01:55, Ville Voutilainen
<ville.voutilainen@gmail.com> wrote:
>
> On Fri, 5 Apr 2019 at 23:29, Jonathan Wakely <jwakely@redhat.com> wrote:
> >
> > On 05/04/19 19:06 +0100, Jonathan Wakely wrote:
> > >       * doc/xml/manual/status_cxx2020.xml: Update status.
> > >       * include/std/variant (visit<R>): Define for C++2a (P0655R1).
> > >       * testsuite/20_util/variant/visit_r.cc: New test.
> >
> > This implementation is wrong, the conversions to R need to happen for
> > each possible invocation, which makes it more complicated (we need to
> > duplicate some code). I'll fix it next week.
>
> Deep sigh. I wanted to postpone this feature until next stage1, but
> now that you have sent soldiers
> to Mirkwood, brace yourself for The Elf. :) Tested on Linux-x64, OK for trunk?
>
> 2019-04-06  Ville Voutilainen  <ville.voutilainen@gmail.com>
>
>     Fix visit<R> for variant.
>     * include/std/variant (__do_visit): Add a template parameter
>     for enforcing same return types for visit.
>     (__gen_vtable_impl): Likewise.
>     (_S_apply_single_alt): Adjust.
>     (__visit_invoke_impl): New. Handle casting to void.
>     (__do_visit_invoke): New. Enforces same return types.
>     (__do_visit_invoke_r): New. Converts return types.
>     (__visit_invoke): Adjust.
>     (__gen_vtable):  Add a template parameter for enforcing
>     same return types for visit.
>     * testsuite/20_util/variant/visit_r.cc: Add a test for a visitor with
>     different return types.
>     * testsuite/20_util/variant/visit_r_neg.cc: New. Ensures that
>     visitors with different return types don't accidentally
>     compile with regular visitation.

And this patch fixes the existing visitation so that we don't
over-eagerly cast to void. The main gist of it is

-        else if constexpr (is_same_v<_Result_type, void>)
+        else if constexpr (!__same_return_types &&
+                          is_same_v<_Result_type, void>)

and the new ensure-test is adjusted to use void as the return type of
the first visitor.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: variant-visit_r_2.diff
Type: text/x-patch
Size: 8732 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20190405/8f504eea/attachment.bin>


More information about the Gcc-patches mailing list