[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,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// constant_wrapper
+
+// [Note 1: The unnamed second template parameter to constant_wrapper is present
+// to aid argument-dependent lookup ([basic.lookup.argdep]) in finding overloads
+// for which constant_wrapper's wrapped value is a suitable argument, but for which
+// the constant_wrapper itself is not. - end note]
+
+#include <cassert>
+#include <concepts>
+#include <type_traits>
+#include <utility>
+
+#include "helpers.h"
+#include "test_macros.h"
+
+namespace MyNamespace {
+struct MyType {
+  int value;
+
+  constexpr MyType(int v = 0) : value(v) {}
+};
+
+constexpr int adl_function(MyType mt) { return mt.value * 2; }
+
+} // namespace MyNamespace
+
+constexpr bool test() {
+  {
+    constexpr MyNamespace::MyType mt{21};
+    std::constant_wrapper<mt> cw_mt;
+
+    std::same_as<int> decltype(auto) result = adl_function(cw_mt);
+    assert(result == 42);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
----------------
ldionne wrote:

I think this should become a `.compile.pass.cpp` test actually -- that would make the intent clearer. The intent is that we're basically just testing that the right ADL thing happens, which is orthogonal to being able to execute the resulting function at compile-time or not.

https://github.com/llvm/llvm-project/pull/191695


More information about the libcxx-commits mailing list