Skip to content

Commit 552f554

Browse files
H-G-HristovZingam
andauthored
[libc++][any] LWG3305: any_cast<void> (#78215)
Implements: https://wg21.link/LWG3305 - https://eel.is/c++draft/any.nonmembers --------- Co-authored-by: Zingam <[email protected]>
1 parent ed276df commit 552f554

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

libcxx/docs/Status/Cxx2cIssues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"","","","","",""
2020
"`2392 <https://wg21.link/LWG2392>`__","""character type"" is used but not defined","Kona November 2023","","",""
2121
"`3203 <https://wg21.link/LWG3203>`__","``span`` element access invalidation","Kona November 2023","","",""
22-
"`3305 <https://wg21.link/LWG3305>`__","``any_cast<void>``","Kona November 2023","","",""
22+
"`3305 <https://wg21.link/LWG3305>`__","``any_cast<void>``","Kona November 2023","|Complete|","18.0",""
2323
"`3431 <https://wg21.link/LWG3431>`__","``<=>`` for containers should require ``three_way_comparable<T>`` instead of ``<=>``","Kona November 2023","","",""
2424
"`3749 <https://wg21.link/LWG3749>`__","``common_iterator`` should handle integer-class difference types","Kona November 2023","","",""
2525
"`3809 <https://wg21.link/LWG3809>`__","Is ``std::subtract_with_carry_engine<uint16_t>`` supposed to work","Kona November 2023","","",""

libcxx/include/any

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ namespace std {
9898
#include <__type_traits/is_nothrow_move_constructible.h>
9999
#include <__type_traits/is_reference.h>
100100
#include <__type_traits/is_same.h>
101+
#include <__type_traits/is_void.h>
101102
#include <__type_traits/remove_cv.h>
102103
#include <__type_traits/remove_cvref.h>
103104
#include <__type_traits/remove_reference.h>
@@ -555,6 +556,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST _ValueType
555556

556557
template <class _ValueType>
557558
inline _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const* __any) _NOEXCEPT {
559+
static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
558560
static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
559561
return std::any_cast<_ValueType>(const_cast<any*>(__any));
560562
}
@@ -572,6 +574,7 @@ inline _LIBCPP_HIDE_FROM_ABI _RetType __pointer_or_func_cast(void*, /*IsFunction
572574
template <class _ValueType>
573575
_LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any* __any) _NOEXCEPT {
574576
using __any_imp::_Action;
577+
static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
575578
static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
576579
typedef add_pointer_t<_ValueType> _ReturnType;
577580
if (__any && __any->__h_) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14
10+
11+
// <any>
12+
13+
// template<class T>
14+
// const T* any_cast(const any* operand) noexcept;
15+
16+
// template<class T>
17+
// T* any_cast(any* operand) noexcept;
18+
19+
#include <any>
20+
21+
void test() {
22+
{
23+
const std::any ca = 1;
24+
25+
// expected-error-re@any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
26+
std::any_cast<void>(&ca); // expected-note {{requested here}}
27+
}
28+
{
29+
std::any a = 1;
30+
31+
// expected-error-re@any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
32+
std::any_cast<void>(&a); // expected-note {{requested here}}
33+
}
34+
}

0 commit comments

Comments
 (0)