Title
Complexity of `std::visit` with immediate functions
Status
new
Section
[variant.visit]
Submitter
Jiang An

Created on 2025-01-26.00:00:00 last changed 3 months ago

Messages

Date: 2025-01-30.16:54:32

Proposed resolution:

This wording is relative to N5001.

  1. Modify [variant.visit] as indicated:

    template<class Visitor, class... Variants>
      constexpr see below visit(Visitor&& vis, Variants&&... vars);
    template<class R, class Visitor, class... Variants>
      constexpr R visit(Visitor&& vis, Variants&&... vars);
    

    -1- […] Let n be `sizeof...(Variants)`. […]

    […]

    -8- Complexity: If n > 1 or any of the aforementioned INVOKE operations calls an immediate function, the invocation of the callable object has no complexity requirements. OtherwiseFor n ≤ 1, the invocation of the callable object is implemented in constant time, i.e., for n = 1, it does not depend on the number of alternative types of V0. For n > 1, the invocation of the callable object has no complexity requirements.

Date: 2025-01-15.00:00:00

[ 2025-01-30; Reflector poll ]

Set priority to 2 after reflector poll.

Date: 2025-01-15.00:00:00

[ 2025-01-01; P3603R0 addresses this ]

Date: 2025-01-26.00:00:00

`std::visit` generally needs to be implemented with "vtables" that contain function pointers. When `std::visit` needs to call an immediate function (e.g. when passing a lambda whose `operator()` is `consteval`), the vtable will contain a pointer to an immediate-escalated function, which forbids the vtable from being a `constexpr` variable.

In order to properly handle immediate functions in `std::visit`, it seems necessary to form the vtable, or do some non-constant-time lookup each time when calling `std::visit`. In other words, [variant.visit]/8 seems to be unimplementable when an immediate function is involved.

History
Date User Action Args
2025-01-30 16:54:32adminsetmessages: + msg14554
2025-01-30 16:54:03adminsetmessages: + msg14553
2025-01-26 14:52:04adminsetmessages: + msg14552
2025-01-26 00:00:00admincreate