|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4197" status="New"> |
| 5 | +<title>Complexity of `std::visit` with immediate functions</title> |
| 6 | +<section><sref ref="[variant.visit]"/></section> |
| 7 | +<submitter>Jiang An</submitter> |
| 8 | +<date>26 Jan 2025</date> |
| 9 | +<priority>99</priority> |
| 10 | + |
| 11 | +<discussion> |
| 12 | +<p> |
| 13 | +`std::visit` generally needs to be implemented with "vtables" that contain function pointers. |
| 14 | +When `std::visit` needs to call an immediate function (e.g. when passing a lambda whose |
| 15 | +`operator()` is `consteval`), the vtable will contain a pointer to an immediate-escalated |
| 16 | +function, which forbids the vtable from being a `constexpr` variable. |
| 17 | +<p/> |
| 18 | +In order to properly handle immediate functions in `std::visit`, it seems necessary to form |
| 19 | +the vtable, or do some non-constant-time lookup each time when calling `std::visit`. In other words, |
| 20 | +<sref ref="[variant.visit]"/>/8 seems to be unimplementable when an immediate function is involved. |
| 21 | +</p> |
| 22 | +</discussion> |
| 23 | + |
| 24 | +<resolution> |
| 25 | +<p> |
| 26 | +This wording is relative to <paper num="N5001"/>. |
| 27 | +</p> |
| 28 | + |
| 29 | +<ol> |
| 30 | + |
| 31 | +<li><p>Modify <sref ref="[variant.visit]"/> as indicated:</p> |
| 32 | + |
| 33 | +<blockquote> |
| 34 | +<pre> |
| 35 | +template<class Visitor, class... Variants> |
| 36 | + constexpr <i>see below</i> visit(Visitor&& vis, Variants&&... vars); |
| 37 | +template<class R, class Visitor, class... Variants> |
| 38 | + constexpr R visit(Visitor&& vis, Variants&&... vars); |
| 39 | +</pre> |
| 40 | +<blockquote> |
| 41 | +<p> |
| 42 | +-1- […] Let <tt><i>n</i></tt> be `sizeof...(Variants)`. […] |
| 43 | +<p/> |
| 44 | +[…] |
| 45 | +<p/> |
| 46 | +-8- <i>Complexity</i>: <ins>If <tt><i>n</i> > 1</tt> or any of the aforementioned <tt><i>INVOKE</i></tt> |
| 47 | +operations calls an immediate function, the invocation of the callable object has no complexity requirements. |
| 48 | +Otherwise</ins><del>For <tt><i>n</i> ≤ 1</tt></del>, the invocation of the callable object is |
| 49 | +implemented in constant time, i.e., for <tt><i>n</i> = 1</tt>, it does not depend on the number |
| 50 | +of alternative types of <tt>V<sub>0</sub></tt>. <del>For <tt><i>n</i> > 1</tt>, the invocation of |
| 51 | +the callable object has no complexity requirements.</del> |
| 52 | +</p> |
| 53 | +</blockquote> |
| 54 | +</blockquote> |
| 55 | +</li> |
| 56 | +</ol> |
| 57 | +</resolution> |
| 58 | + |
| 59 | +</issue> |
0 commit comments