[libcxx-commits] [libcxx] [libc++] Fix constant_wrapper::operator() (PR #193573)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Apr 25 02:29:34 PDT 2026
https://github.com/huixie90 updated https://github.com/llvm/llvm-project/pull/193573
>From 4d7c4c3bb4bf9643ad2aec1eb921046ed646056a Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Wed, 22 Apr 2026 20:41:24 +0100
Subject: [PATCH 1/3] [libc++] Fix constant_wrapper::operator()
---
libcxx/include/__utility/constant_wrapper.h | 68 +++++++++----------
.../const.wrap.class/nodiscard.verify.cpp | 9 +++
.../utilities/const.wrap.class/call.pass.cpp | 18 +++++
.../const.wrap.class/subscript.pass.cpp | 18 +++++
4 files changed, 78 insertions(+), 35 deletions(-)
diff --git a/libcxx/include/__utility/constant_wrapper.h b/libcxx/include/__utility/constant_wrapper.h
index c3e2f5e400980..fae6b535353aa 100644
--- a/libcxx/include/__utility/constant_wrapper.h
+++ b/libcxx/include/__utility/constant_wrapper.h
@@ -12,8 +12,10 @@
#include <__config>
#include <__cstddef/size_t.h>
#include <__functional/invoke.h>
+#include <__type_traits/invoke.h>
#include <__type_traits/is_constructible.h>
#include <__type_traits/remove_cvref.h>
+#include <__utility/declval.h>
#include <__utility/forward.h>
#include <__utility/integer_sequence.h>
@@ -276,62 +278,58 @@ struct __cw_operators {
}
};
+template <const auto& __callbale, class... _Args>
+concept __constexpr_callable = (__constexpr_param<remove_cvref_t<_Args>> && ...) && requires {
+ typename constant_wrapper<std::invoke(__callbale, remove_cvref_t<_Args>::value...)>;
+};
+
+template <const auto& __obj, class... _Args>
+concept __constexpr_indexable = (__constexpr_param<remove_cvref_t<_Args>> && ...) && requires {
+ typename constant_wrapper<__obj[remove_cvref_t<_Args>::value...]>;
+};
+
template <__cw_fixed_value _Xp, class>
struct constant_wrapper : __cw_operators {
static constexpr const auto& value = _Xp.__data;
using type = constant_wrapper;
using value_type = decltype(_Xp)::__type;
-private:
- 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...)> {
+ template <__constexpr_param _Rp>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator=(_Rp) const noexcept
+ -> constant_wrapper<(value = _Rp::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)...);
- }
+ _LIBCPP_HIDE_FROM_ABI constexpr operator decltype(value)() const noexcept { return value; }
template <class... _Args>
- requires(__constexpr_param<remove_cvref_t<_Args>> && ...)
- _LIBCPP_HIDE_FROM_ABI static constexpr auto __subscript(_Args&&...) noexcept
- -> constant_wrapper<value[remove_cvref_t<_Args>::value...]> {
+ requires __constexpr_callable<value, _Args...>
+ [[nodiscard]]
+ _LIBCPP_HIDE_FROM_ABI static constexpr constant_wrapper<std::invoke(value, remove_cvref_t<_Args>::value...)>
+ operator()(_Args&&... __args) noexcept {
return {};
}
template <class... _Args>
- _LIBCPP_HIDE_FROM_ABI static constexpr auto
- __subscript(_Args&&... __args) noexcept(noexcept(value[std::forward<_Args>(__args)...]))
- -> decltype(value[std::forward<_Args>(__args)...]) {
- return value[std::forward<_Args>(__args)...];
- }
-
-public:
- template <__constexpr_param _Rp>
- [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator=(_Rp) const noexcept
- -> constant_wrapper<(value = _Rp::value)> {
- return {};
+ requires(!__constexpr_callable<value, _Args...> && is_invocable_v<const value_type&, _Args && ...>)
+ _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
+ operator()(_Args&&... __args) noexcept(noexcept(std::invoke(value, std::forward<_Args>(__args)...))) {
+ return std::invoke(value, std::forward<_Args>(__args)...);
}
- _LIBCPP_HIDE_FROM_ABI constexpr operator decltype(value)() const noexcept { return value; }
-
template <class... _Args>
- _LIBCPP_HIDE_FROM_ABI static constexpr auto
- operator()(_Args&&... __args) noexcept(noexcept(constant_wrapper::__call(std::forward<_Args>(__args)...)))
- -> decltype(constant_wrapper::__call(std::forward<_Args>(__args)...)) {
- return __call(std::forward<_Args>(__args)...);
+ requires __constexpr_indexable<value, _Args...>
+ [[nodiscard]]
+ _LIBCPP_HIDE_FROM_ABI static constexpr constant_wrapper<value[remove_cvref_t<_Args>::value...]>
+ operator[](_Args&&...) noexcept {
+ return {};
}
template <class... _Args>
- _LIBCPP_HIDE_FROM_ABI static constexpr auto
- operator[](_Args&&... __args) noexcept(noexcept(constant_wrapper::__subscript(std::forward<_Args>(__args)...)))
- -> decltype(constant_wrapper::__subscript(std::forward<_Args>(__args)...)) {
- return __subscript(std::forward<_Args>(__args)...);
+ requires(!__constexpr_indexable<value, _Args...> && requires { value[std::declval<_Args>()...]; })
+ _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto)
+ operator[](_Args&&... __args) noexcept(noexcept(value[std::forward<_Args>(__args)...])) {
+ return value[std::forward<_Args>(__args)...];
}
};
diff --git a/libcxx/test/libcxx/utilities/const.wrap.class/nodiscard.verify.cpp b/libcxx/test/libcxx/utilities/const.wrap.class/nodiscard.verify.cpp
index 1b8ac6ecc4cca..7e2a7e4f6e828 100644
--- a/libcxx/test/libcxx/utilities/const.wrap.class/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/utilities/const.wrap.class/nodiscard.verify.cpp
@@ -35,6 +35,9 @@ struct Ops {
constexpr Ops operator>>=(Ops) const { return {}; }
constexpr Ops operator=(auto) const { return {}; }
+
+ constexpr Ops operator()(Ops) const { return {}; }
+ constexpr Ops operator[](Ops) const { return {}; }
};
void test() {
@@ -140,4 +143,10 @@ void test() {
std::constant_wrapper<Ops{}> a;
// expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
a = std::cw<5>;
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::cw<Ops{}>(std::cw<Ops{}>);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::cw<Ops{}>[std::cw<Ops{}>];
}
diff --git a/libcxx/test/std/utilities/const.wrap.class/call.pass.cpp b/libcxx/test/std/utilities/const.wrap.class/call.pass.cpp
index d63a92f62c6ff..dfa8813e47a8d 100644
--- a/libcxx/test/std/utilities/const.wrap.class/call.pass.cpp
+++ b/libcxx/test/std/utilities/const.wrap.class/call.pass.cpp
@@ -82,6 +82,18 @@ static_assert(std::is_nothrow_invocable_v<std::constant_wrapper<throwing_call>,
"the call expression is still nothrow because the constexpr path is taken");
// clang-format on
+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 {};
+ }
+};
+
constexpr bool test() {
{
// with runtime param
@@ -227,6 +239,12 @@ constexpr bool test() {
static_assert(result == 3);
}
+ {
+ using T = std::constant_wrapper<Poison{}>;
+ [[maybe_unused]] std::same_as<std::constant_wrapper<MustBeInt<int>{}>> decltype(auto) result =
+ T::operator()(std::cw<5>);
+ }
+
return true;
}
diff --git a/libcxx/test/std/utilities/const.wrap.class/subscript.pass.cpp b/libcxx/test/std/utilities/const.wrap.class/subscript.pass.cpp
index 697f31cb1df04..7efefa85fed95 100644
--- a/libcxx/test/std/utilities/const.wrap.class/subscript.pass.cpp
+++ b/libcxx/test/std/utilities/const.wrap.class/subscript.pass.cpp
@@ -85,6 +85,18 @@ static_assert(!HasNothrowSubscript<std::constant_wrapper<ThrowingSubscript{}>, i
static_assert(HasNothrowSubscript<std::constant_wrapper<ThrowingSubscript{}>, std::constant_wrapper<1>>,
"the subscript expression is still nothrow because the constexpr path is taken");
+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 {};
+ }
+};
+
constexpr bool test() {
{
// with runtime param
@@ -172,6 +184,12 @@ constexpr bool test() {
static_assert(result == 2);
}
+ {
+ using T = std::constant_wrapper<Poison{}>;
+ [[maybe_unused]] std::same_as<std::constant_wrapper<MustBeInt<int>{}>> decltype(auto) result =
+ T::operator[](std::cw<5>);
+ }
+
return true;
}
>From eeb095f6c7260402bc5c52956a7ec7e06085b5ce Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Wed, 22 Apr 2026 21:00:06 +0100
Subject: [PATCH 2/3] ci
---
libcxx/include/__utility/constant_wrapper.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/__utility/constant_wrapper.h b/libcxx/include/__utility/constant_wrapper.h
index fae6b535353aa..6cbdc27f3a768 100644
--- a/libcxx/include/__utility/constant_wrapper.h
+++ b/libcxx/include/__utility/constant_wrapper.h
@@ -306,7 +306,7 @@ struct constant_wrapper : __cw_operators {
requires __constexpr_callable<value, _Args...>
[[nodiscard]]
_LIBCPP_HIDE_FROM_ABI static constexpr constant_wrapper<std::invoke(value, remove_cvref_t<_Args>::value...)>
- operator()(_Args&&... __args) noexcept {
+ operator()(_Args&&...) noexcept {
return {};
}
>From d5d1ef0595904638f102d2ba63cbeb91af65d3b8 Mon Sep 17 00:00:00 2001
From: Hui Xie <hui.xie1990 at gmail.com>
Date: Sat, 25 Apr 2026 10:29:21 +0100
Subject: [PATCH 3/3] review
---
libcxx/include/__utility/constant_wrapper.h | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/libcxx/include/__utility/constant_wrapper.h b/libcxx/include/__utility/constant_wrapper.h
index 6cbdc27f3a768..6bae95fc1878a 100644
--- a/libcxx/include/__utility/constant_wrapper.h
+++ b/libcxx/include/__utility/constant_wrapper.h
@@ -278,14 +278,14 @@ struct __cw_operators {
}
};
-template <const auto& __callbale, class... _Args>
+template <const auto& _Callable, class... _Args>
concept __constexpr_callable = (__constexpr_param<remove_cvref_t<_Args>> && ...) && requires {
- typename constant_wrapper<std::invoke(__callbale, remove_cvref_t<_Args>::value...)>;
+ typename constant_wrapper<std::invoke(_Callable, remove_cvref_t<_Args>::value...)>;
};
-template <const auto& __obj, class... _Args>
+template <const auto& _Obj, class... _Args>
concept __constexpr_indexable = (__constexpr_param<remove_cvref_t<_Args>> && ...) && requires {
- typename constant_wrapper<__obj[remove_cvref_t<_Args>::value...]>;
+ typename constant_wrapper<_Obj[remove_cvref_t<_Args>::value...]>;
};
template <__cw_fixed_value _Xp, class>
More information about the libcxx-commits
mailing list