Bug 118434 - std::visit should be updated to handle immediate functions
Summary: std::visit should be updated to handle immediate functions
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 15.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2025-01-12 13:58 UTC by Jiang An
Modified: 2025-01-30 13:55 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jiang An 2025-01-12 13:58:15 UTC
It seems that the following example should be valid since C++20 (patched by P2564R3).

```
#include <variant>

consteval auto func(const std::variant<int>& v1, const std::variant<int>& v2) {
    return std::visit([](auto x, auto y) consteval { return x + y; }, v1, v2);
}

static_assert(func(std::variant<int>{42}, std::variant<int>{1729}) == 1771);
```

But libstdc++ currently rejects it (https://godbolt.org/z/h59hWK9G3), because the there's a hand-made vtable which will contain pointers to immediate (immediate-escalated) functions while being constexpr.

I guess we can make the vtable a non-constexpr variable of automatic storage duration when the vtable can't be constexpr. It seems that we can determine this by testing that `__some_alias_template<__gen_vtable_impl<...>::_S_apply>()>` is not a valid type.
Comment 1 Jiang An 2025-01-12 13:58:49 UTC
Corresponding libc++ issue: https://github.com/llvm/llvm-project/issues/118560
Comment 2 Jonathan Wakely 2025-01-30 13:55:37 UTC
https://cplusplus.github.io/LWG/issue4197