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

Jonathan Wakely jwakely@redhat.com
Wed Apr 10 21:27:00 GMT 2019


On 08/04/19 19:54 +0100, Jonathan Wakely wrote:
>On 08/04/19 17:36 +0100, Jonathan Wakely wrote:
>>On 08/04/19 19:20 +0300, Ville Voutilainen wrote:
>>>On Mon, 8 Apr 2019 at 19:12, Ville Voutilainen
>>><ville.voutilainen@gmail.com> wrote:
>>>>
>>>>On Mon, 8 Apr 2019 at 19:02, Jonathan Wakely <jwakely@redhat.com> wrote:
>>>>>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?
>>>>
>>>>I hate the duplication of __gen_vtable with a burning passion, because
>>>>*that* is the part that causes me heartburn,
>>>>not the compile-time ifs in the other bits. But if this is what you
>>>>want to ship, I can live with it.
>>>
>>>A bit of elaboration: in all of this, the most dreadful parts to
>>>understand were _Multi_array and __gen_vtable.
>>
>>Completely agreed, that's why I found extending __gen_vtable_impl to
>>handle a new feature made it even harder to understand.
>
>Whereas the attached patch *removes* code from __gen_vtable, but fixes
>the bug in my original std::visit<R> implementation, *and* fixes a bug
>in __visitor_result_type (it doesn't use INVOKE).
>
>The downside of this patch is that it fails to diagnose some
>ill-formed visitors where not all invocations return the same type. So
>it's not acceptable. But I still think there's a simpler design
>struggling to get out.

Here's a version that doesn't have that problem.

This adds std::__invoke_r to implement INVOKE<R>, and then uses it in
std::bind, std::function, std::packaged_task and std::visit<R>. Having
a helper for INVOKE<R>  simplifies a number of things, including the
visitation code in <variant>. It also means that we'll only need to
change one place to implement https://wg21.link/p0932r0 once a revised
version of that gets accepted into the draft.

I'll commit this (in a series of patches) in stage 1.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 30913 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20190410/39589a3f/attachment.bin>


More information about the Gcc-patches mailing list