[libcxx-commits] [libcxx] 59681c6 - [libc++] remove duplicate assertions for void/reference const any_cast (#199425)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon May 25 04:42:14 PDT 2026
Author: Matheus Izvekov
Date: 2026-05-25T11:42:09Z
New Revision: 59681c65c71c4f89bf86e010291ac6f5d7cd5003
URL: https://github.com/llvm/llvm-project/commit/59681c65c71c4f89bf86e010291ac6f5d7cd5003
DIFF: https://github.com/llvm/llvm-project/commit/59681c65c71c4f89bf86e010291ac6f5d7cd5003.diff
LOG: [libc++] remove duplicate assertions for void/reference const any_cast (#199425)
For test cases of the const overload of any_cast, such as:
```C++
void test() {
std::any a = 0;
const std::any& a2 = a;
(void)std::any_cast<int&>(&a2);
}
```
(And similarly for void).
The problem is that the assertions are implemented both in the const and
non-const any_cast overloads, but since the const overload delegates to
the non-const overload, that ends up producing the same assertion twice.
This separates those test cases, because those assertions are
implemented in the function body, and that's only instantiated once per
specialization, not once per use.
Added:
libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_reference_types.verify.cpp
libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.const.verify.cpp
Modified:
libcxx/include/any
libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp
libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp
Removed:
################################################################################
diff --git a/libcxx/include/any b/libcxx/include/any
index 740bcec93835c..4addb3c2b8835 100644
--- a/libcxx/include/any
+++ b/libcxx/include/any
@@ -526,8 +526,6 @@ template <class _ValueType>
template <class _ValueType>
[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const* __any) noexcept {
- static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
- static_assert(!is_reference_v<_ValueType>, "_ValueType may not be a reference.");
return std::any_cast<_ValueType>(const_cast<any*>(__any));
}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_reference_types.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_reference_types.verify.cpp
new file mode 100644
index 0000000000000..cf8a7f4e77a52
--- /dev/null
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_reference_types.verify.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++17
+
+// <any>
+
+// template <class ValueType>
+// ValueType const* any_cast(any const *) noexcept;
+
+#include <any>
+
+void test() {
+ std::any a1 = 1;
+ const std::any& a = a1;
+
+ // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ (void)std::any_cast<int&>(&a); // expected-note {{requested here}}
+
+ // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ (void)std::any_cast<int&&>(&a); // expected-note {{requested here}}
+
+ // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ (void)std::any_cast<int const&>(&a); // expected-note {{requested here}}
+
+ // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
+ (void)std::any_cast<int const&&>(&a); // expected-note {{requested here}}
+}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp
index d634f73291e0e..e034a749569ca 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp
@@ -10,9 +10,6 @@
// <any>
-// template <class ValueType>
-// ValueType const* any_cast(any const *) noexcept;
-//
// template <class ValueType>
// ValueType * any_cast(any *) noexcept;
@@ -32,18 +29,4 @@ void test() {
// expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
(void)std::any_cast<int const&&>(&a); // expected-note {{requested here}}
-
- const std::any& a2 = a;
-
- // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- (void)std::any_cast<int&>(&a2); // expected-note {{requested here}}
-
- // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- (void)std::any_cast<int&&>(&a2); // expected-note {{requested here}}
-
- // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- (void)std::any_cast<int const&>(&a2); // expected-note {{requested here}}
-
- // expected-error-re at any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
- (void)std::any_cast<int const&&>(&a2); // expected-note {{requested here}}
}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.const.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.const.verify.cpp
new file mode 100644
index 0000000000000..3dfc3ee2f4991
--- /dev/null
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.const.verify.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++17
+
+// <any>
+
+// template<class T>
+// const T* any_cast(const any* operand) noexcept;
+
+#include <any>
+
+void test() {
+ const std::any ca = 1;
+
+ // expected-error-re at any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
+ (void)std::any_cast<void>(&ca); // expected-note {{requested here}}
+}
diff --git a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp
index d1ec42eaf10c2..c3d1530871d50 100644
--- a/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp
+++ b/libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp
@@ -10,25 +10,14 @@
// <any>
-// template<class T>
-// const T* any_cast(const any* operand) noexcept;
-
// template<class T>
// T* any_cast(any* operand) noexcept;
#include <any>
void test() {
- {
- const std::any ca = 1;
-
- // expected-error-re at any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
- (void)std::any_cast<void>(&ca); // expected-note {{requested here}}
- }
- {
- std::any a = 1;
+ std::any a = 1;
- // expected-error-re at any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
- (void)std::any_cast<void>(&a); // expected-note {{requested here}}
- }
+ // expected-error-re at any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
+ (void)std::any_cast<void>(&a); // expected-note {{requested here}}
}
More information about the libcxx-commits
mailing list