Files
Hui 44633491b7 [libc++] Fix constant_wrapper::operator() (#193573)
As Tomasz pointed out on mattermost, 
given

```cpp
template <class T>
struct MustBeInt {
  static_assert(std::same_as<T, int>);
};

struct Poison {
  template <class T>
  constexpr auto operator()(T) const noexcept -> MustBeInt<T> {
    return {};
  }
};

std::cw<Poison{}>(std::cw<5>);
```

This should work according to the wording
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3978r3.pdf

```cpp
template<class... Args>
static constexpr decltype(auto) operator()(Args&&... args) noexcept(see below);
```

```
- Let call-expr be constant_wrapper<INVOKE (value, remove_cvref_t<Args>::value...)>{} if all types
in remove_cvref_t<Args>... satisfy constexpr-param and constant_wrapper<INVOKE (value, remove_-
cvref_t<Args>::value...)> is a valid type, otherwise let call-expr be INVOKE (value, std::forward<Args>(args)...).
- Constraints: call-expr is a valid expression.
- Effects: Equivalent to: return call-expr;
- Remarks: The exception specification is equivalent to noexcept(call-expr).
```

so basically the spec says, there are two cases
- constexpr-param case
-  runtime case

and if constexpr-param case works, uses it, otherwise fallback to
runtime case if it is valid, otherwise substitution error.

In this case, `std::cw<5>` satisfy `constexpr-param`, and
`constant_wrapper<Poison{}(5)>` is valid , it should just work. however,
our implementation implemented the two cases in two overload

```cpp
template <class... _Args>
    requires(__constexpr_param<remove_cvref_t<_Args>> && ...)
  _LIBCPP_HIDE_FROM_ABI static constexpr auto __call(_Args&&...) noexcept
      -> constant_wrapper<std::invoke(value, remove_cvref_t<_Args>::value...)> {
  return {};
  }

 template <class... _Args>
  _LIBCPP_HIDE_FROM_ABI static constexpr auto
  __call(_Args&&... __args) noexcept(noexcept(std::invoke(value, std::forward<_Args>(__args)...)))
      -> decltype(std::invoke(value, std::forward<_Args>(__args)...)) {
    return std::invoke(value, std::forward<_Args>(__args)...);
  }
```

The logic is that, both overloads use return type SFINAE and the
constexpr-param case's concept is more constrained so it will be picked
first if constexpr-param case works.

However, in this example, the second overload's return type SFINAE
causes a hard error by the `static_assert` in the user code.

The fix is simply constrain the second overload and not using return
type SFINAE so it short circuits if constexpr-param case is valid.


As a drive by, removed the internal helper function and directly make
the public interface `operator()` two overloads so that we can apply
`[[nodiscard]]` on the constexpr-param case
2026-04-25 14:20:40 +01:00
..