[PATCH] Implement std::visit<R> for C++2a (P0655R1)
Jonathan Wakely
jwakely@redhat.com
Mon Apr 8 16:02:00 GMT 2019
On 06/04/19 03:07 +0300, Ville Voutilainen wrote:
>On Sat, 6 Apr 2019 at 02:55, Ville Voutilainen
><ville.voutilainen@gmail.com> wrote:
>
>> Just in case that cast looks scary: the implicit conversion is also
>> deep down in __visit_invoke, so
>> we do actually require implicit convertibility as we are supposed to.
>> If that's still too scary,
>> we can just do
>>
>> - return (_Res)
>> + if constexpr (!is_void_v<_Res>)
>> + return
>> + __do_visit<false, false>(std::forward<_Visitor>(__visitor),
>> + std::forward<_Variants>(__variants)...);
>> + else
>>
>> like in the attached patch.
>
>Okay, I merged that with the original. I also renamed the neg-test, so
>here goes the hopefully final variant change for GCC 9:
>
>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_neg.cc: New. Ensures that
> visitors with different return types don't accidentally
> compile with regular visitation.
I'm concerned about how dense and impenetrable this code is (the
absence of comments certainly doesn't help).
The attached patch implements the same thing with totally separate
__gen_vtable_r and __gen_vtable_r_impl class templates, instead of
adding all the visit<R> functionality into the existing code (and then
needing to tease it apart again with if-constexpr).
The visit<R> implementation doesn't need to care about the
__variant_cookie or __variant_idx_cookie cases, which simplifies
things.
This also adjusts some whitespace, for correct indentation and for
readability. And removes a redundant && from a type.
What do you think?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 7385 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20190408/efbb66d1/attachment.bin>
More information about the Libstdc++
mailing list