[libcxx-commits] [libcxx] [libcxx] Implement `std::constant_wrapper` (PR #191695)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Apr 17 11:06:15 PDT 2026
================
@@ -0,0 +1,590 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+// ADDITIONAL_COMPILE_FLAGS: -Wno-constant-logical-operand
+
+// constant_wrapper
+
+// template<constexpr-param L, constexpr-param R>
+// friend constexpr auto operator+(L, R) noexcept -> constant_wrapper<(L::value + R::value)>
+// { return {}; }
+// template<constexpr-param L, constexpr-param R>
+// friend constexpr auto operator-(L, R) noexcept -> constant_wrapper<(L::value - R::value)>
+// { return {}; }
+// template<constexpr-param L, constexpr-param R>
+// friend constexpr auto operator*(L, R) noexcept -> constant_wrapper<(L::value * R::value)>
+// { return {}; }
+// template<constexpr-param L, constexpr-param R>
+// friend constexpr auto operator/(L, R) noexcept -> constant_wrapper<(L::value / R::value)>
+// { return {}; }
+// template<constexpr-param L, constexpr-param R>
+// friend constexpr auto operator%(L, R) noexcept -> constant_wrapper<(L::value % R::value)>
+// { return {}; }
+
+// template<constexpr-param L, constexpr-param R>
+// friend constexpr auto operator<<(L, R) noexcept -> constant_wrapper<(L::value << R::value)>
+// { return {}; }
+// template<constexpr-param L, constexpr-param R>
+// friend constexpr auto operator>>(L, R) noexcept -> constant_wrapper<(L::value >> R::value)>
+// { return {}; }
+// template<constexpr-param L, constexpr-param R>
+// friend constexpr auto operator&(L, R) noexcept -> constant_wrapper<(L::value & R::value)>
+// { return {}; }
+// template<constexpr-param L, constexpr-param R>
+// friend constexpr auto operator|(L, R) noexcept -> constant_wrapper<(L::value | R::value)>
+// { return {}; }
+// template<constexpr-param L, constexpr-param R>
+// friend constexpr auto operator^(L, R) noexcept -> constant_wrapper<(L::value ^ R::value)>
+// { return {}; }
+
+// template<constexpr-param L, constexpr-param R>
+// requires (!is_constructible_v<bool, decltype(L::value)> ||
+// !is_constructible_v<bool, decltype(R::value)>)
+// friend constexpr auto operator&&(L, R) noexcept
+// -> constant_wrapper<(L::value && R::value)>
+// { return {}; }
+// template<constexpr-param L, constexpr-param R>
+// requires (!is_constructible_v<bool, decltype(L::value)> ||
+// !is_constructible_v<bool, decltype(R::value)>)
+// friend constexpr auto operator||(L, R) noexcept
+// -> constant_wrapper<(L::value || R::value)>
+// { return {}; }
+
+#include <cassert>
+#include <concepts>
+#include <functional>
+#include <type_traits>
+#include <utility>
+
+#include "helpers.h"
+#include "test_macros.h"
+
+struct WithOps {
+ int value;
+
+ constexpr WithOps(int v) : value(v) {}
+
+ friend constexpr auto operator+(WithOps l, WithOps r) { return WithOps{l.value + r.value}; }
+ friend constexpr auto operator-(WithOps l, WithOps r) { return WithOps{l.value - r.value}; }
+ friend constexpr auto operator*(WithOps l, WithOps r) { return WithOps{l.value * r.value}; }
+ friend constexpr auto operator/(WithOps l, WithOps r) { return WithOps{l.value / r.value}; }
+ friend constexpr auto operator%(WithOps l, WithOps r) { return WithOps{l.value % r.value}; }
+ friend constexpr auto operator<<(WithOps l, WithOps r) { return WithOps{l.value << r.value}; }
+ friend constexpr auto operator>>(WithOps l, WithOps r) { return WithOps{l.value >> r.value}; }
+ friend constexpr auto operator&(WithOps l, WithOps r) { return WithOps{l.value & r.value}; }
+ friend constexpr auto operator|(WithOps l, WithOps r) { return WithOps{l.value | r.value}; }
+ friend constexpr auto operator^(WithOps l, WithOps r) { return WithOps{l.value ^ r.value}; }
+
+ friend constexpr auto operator&&(WithOps l, WithOps r) { return WithOps{l.value && r.value}; }
+ friend constexpr auto operator||(WithOps l, WithOps r) { return WithOps{l.value || r.value}; }
+};
+
+struct OptsReturnNonStructural {
----------------
ldionne wrote:
```suggestion
struct OpsReturnNonStructural {
```
https://github.com/llvm/llvm-project/pull/191695
More information about the libcxx-commits
mailing list