Skip to content

Commit 170810f

Browse files
authored
P1957R2: remove special-case for booleans in std::variant (#71502)
This picks up the std::variant half of P1957R2. Fixes #62332.
1 parent b25d36c commit 170810f

File tree

6 files changed

+28
-27
lines changed

6 files changed

+28
-27
lines changed

libcxx/docs/Status/Cxx20Papers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
"`P1868R2 <https://wg21.link/P1868R2>`__","LWG","width: clarifying units of width and precision in std::format","Prague","|Complete|","14.0"
174174
"`P1937R2 <https://wg21.link/P1937R2>`__","CWG","Fixing inconsistencies between constexpr and consteval functions","Prague","* *",""
175175
"`P1956R1 <https://wg21.link/P1956R1>`__","LWG","On the names of low-level bit manipulation functions","Prague","|Complete|","12.0"
176-
"`P1957R2 <https://wg21.link/P1957R2>`__","CWG","Converting from ``T*``\ to bool should be considered narrowing (re: US 212)","Prague","* *",""
176+
"`P1957R2 <https://wg21.link/P1957R2>`__","CWG","Converting from ``T*``\ to bool should be considered narrowing (re: US 212)","Prague","|Complete|","18.0"
177177
"`P1963R0 <https://wg21.link/P1963R0>`__","LWG","Fixing US 313","Prague","* *","",""
178178
"`P1964R2 <https://wg21.link/P1964R2>`__","LWG","Wording for boolean-testable","Prague","|Complete|","13.0"
179179
"`P1970R2 <https://wg21.link/P1970R2>`__","LWG","Consistency for size() functions: Add ranges::ssize","Prague","|Complete|","15.0","|ranges|"

libcxx/include/variant

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,22 +1252,6 @@ struct __overload {
12521252
auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
12531253
};
12541254

1255-
template <class _Tp, size_t>
1256-
struct __overload_bool {
1257-
template <class _Up, class _Ap = __remove_cvref_t<_Up>>
1258-
auto operator()(bool, _Up&&) const
1259-
-> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>;
1260-
};
1261-
1262-
template <size_t _Idx>
1263-
struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
1264-
template <size_t _Idx>
1265-
struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
1266-
template <size_t _Idx>
1267-
struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
1268-
template <size_t _Idx>
1269-
struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};
1270-
12711255
template <class ..._Bases>
12721256
struct __all_overloads : _Bases... {
12731257
void operator()() const;

libcxx/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <string>
2020
#include <type_traits>
2121
#include <variant>
22+
#include <vector>
2223
#include <memory>
2324

2425
#include "test_macros.h"
@@ -145,8 +146,8 @@ void test_T_assignment_sfinae() {
145146
};
146147
static_assert(!std::is_assignable<V, X>::value,
147148
"no boolean conversion in operator=");
148-
static_assert(!std::is_assignable<V, std::false_type>::value,
149-
"no converted to bool in operator=");
149+
static_assert(std::is_assignable<V, std::false_type>::value,
150+
"converted to bool in operator=");
150151
}
151152
{
152153
struct X {};
@@ -295,12 +296,21 @@ void test_T_assignment_performs_assignment() {
295296
#endif // TEST_HAS_NO_EXCEPTIONS
296297
}
297298

299+
void test_T_assignment_vector_bool() {
300+
std::vector<bool> vec = {true};
301+
std::variant<bool, int> v;
302+
v = vec[0];
303+
assert(v.index() == 0);
304+
assert(std::get<0>(v) == true);
305+
}
306+
298307
int main(int, char**) {
299308
test_T_assignment_basic();
300309
test_T_assignment_performs_construction();
301310
test_T_assignment_performs_assignment();
302311
test_T_assignment_noexcept();
303312
test_T_assignment_sfinae();
313+
test_T_assignment_vector_bool();
304314

305315
return 0;
306316
}

libcxx/test/std/utilities/variant/variant.variant/variant.assign/conv.pass.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ int main(int, char**)
3333

3434
static_assert(!std::is_assignable<std::variant<int, bool>, decltype("meow")>::value, "");
3535
static_assert(!std::is_assignable<std::variant<int, const bool>, decltype("meow")>::value, "");
36-
static_assert(!std::is_assignable<std::variant<int, const volatile bool>, decltype("meow")>::value, "");
3736

38-
static_assert(!std::is_assignable<std::variant<bool>, std::true_type>::value, "");
37+
static_assert(std::is_assignable<std::variant<bool>, std::true_type>::value, "");
3938
static_assert(!std::is_assignable<std::variant<bool>, std::unique_ptr<char> >::value, "");
4039
static_assert(!std::is_assignable<std::variant<bool>, decltype(nullptr)>::value, "");
4140

libcxx/test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <type_traits>
2020
#include <variant>
2121
#include <memory>
22+
#include <vector>
2223

2324
#include "test_macros.h"
2425
#include "variant_test_helpers.h"
@@ -79,8 +80,8 @@ void test_T_ctor_sfinae() {
7980
};
8081
static_assert(!std::is_constructible<V, X>::value,
8182
"no boolean conversion in constructor");
82-
static_assert(!std::is_constructible<V, std::false_type>::value,
83-
"no converted to bool in constructor");
83+
static_assert(std::is_constructible<V, std::false_type>::value,
84+
"converted to bool in constructor");
8485
}
8586
{
8687
struct X {};
@@ -138,12 +139,12 @@ void test_T_ctor_basic() {
138139
assert(std::get<0>(v) == "foo");
139140
}
140141
{
141-
std::variant<bool volatile, std::unique_ptr<int>> v = nullptr;
142+
std::variant<bool, std::unique_ptr<int>> v = nullptr;
142143
assert(v.index() == 1);
143144
assert(std::get<1>(v) == nullptr);
144145
}
145146
{
146-
std::variant<bool volatile const, int> v = true;
147+
std::variant<bool const, int> v = true;
147148
assert(v.index() == 0);
148149
assert(std::get<0>(v));
149150
}
@@ -198,11 +199,19 @@ void test_construction_with_repeated_types() {
198199
static_assert(std::is_constructible<V, Bar>::value, "");
199200
}
200201

202+
void test_vector_bool() {
203+
std::vector<bool> vec = {true};
204+
std::variant<bool, int> v = vec[0];
205+
assert(v.index() == 0);
206+
assert(std::get<0>(v) == true);
207+
}
208+
201209
int main(int, char**) {
202210
test_T_ctor_basic();
203211
test_T_ctor_noexcept();
204212
test_T_ctor_sfinae();
205213
test_no_narrowing_check_for_class_types();
206214
test_construction_with_repeated_types();
215+
test_vector_bool();
207216
return 0;
208217
}

libcxx/test/std/utilities/variant/variant.variant/variant.ctor/conv.pass.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ int main(int, char**)
3232

3333
static_assert(!std::is_constructible<std::variant<int, bool>, decltype("meow")>::value, "");
3434
static_assert(!std::is_constructible<std::variant<int, const bool>, decltype("meow")>::value, "");
35-
static_assert(!std::is_constructible<std::variant<int, const volatile bool>, decltype("meow")>::value, "");
3635

37-
static_assert(!std::is_constructible<std::variant<bool>, std::true_type>::value, "");
36+
static_assert(std::is_constructible<std::variant<bool>, std::true_type>::value, "");
3837
static_assert(!std::is_constructible<std::variant<bool>, std::unique_ptr<char> >::value, "");
3938
static_assert(!std::is_constructible<std::variant<bool>, decltype(nullptr)>::value, "");
4039

0 commit comments

Comments
 (0)