Skip to content

Commit

Permalink
P1957R2: remove special-case for booleans in std::variant (#71502)
Browse files Browse the repository at this point in the history
This picks up the std::variant half of P1957R2. Fixes #62332.
  • Loading branch information
davidben authored Nov 15, 2023
1 parent b25d36c commit 170810f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion libcxx/docs/Status/Cxx20Papers.csv
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
"`P1868R2 <https://wg21.link/P1868R2>`__","LWG","width: clarifying units of width and precision in std::format","Prague","|Complete|","14.0"
"`P1937R2 <https://wg21.link/P1937R2>`__","CWG","Fixing inconsistencies between constexpr and consteval functions","Prague","* *",""
"`P1956R1 <https://wg21.link/P1956R1>`__","LWG","On the names of low-level bit manipulation functions","Prague","|Complete|","12.0"
"`P1957R2 <https://wg21.link/P1957R2>`__","CWG","Converting from ``T*``\ to bool should be considered narrowing (re: US 212)","Prague","* *",""
"`P1957R2 <https://wg21.link/P1957R2>`__","CWG","Converting from ``T*``\ to bool should be considered narrowing (re: US 212)","Prague","|Complete|","18.0"
"`P1963R0 <https://wg21.link/P1963R0>`__","LWG","Fixing US 313","Prague","* *","",""
"`P1964R2 <https://wg21.link/P1964R2>`__","LWG","Wording for boolean-testable","Prague","|Complete|","13.0"
"`P1970R2 <https://wg21.link/P1970R2>`__","LWG","Consistency for size() functions: Add ranges::ssize","Prague","|Complete|","15.0","|ranges|"
Expand Down
16 changes: 0 additions & 16 deletions libcxx/include/variant
Original file line number Diff line number Diff line change
Expand Up @@ -1252,22 +1252,6 @@ struct __overload {
auto operator()(_Tp, _Up&&) const -> __check_for_narrowing<_Tp, _Up>;
};

template <class _Tp, size_t>
struct __overload_bool {
template <class _Up, class _Ap = __remove_cvref_t<_Up>>
auto operator()(bool, _Up&&) const
-> enable_if_t<is_same_v<_Ap, bool>, __type_identity<_Tp>>;
};

template <size_t _Idx>
struct __overload<bool, _Idx> : __overload_bool<bool, _Idx> {};
template <size_t _Idx>
struct __overload<bool const, _Idx> : __overload_bool<bool const, _Idx> {};
template <size_t _Idx>
struct __overload<bool volatile, _Idx> : __overload_bool<bool volatile, _Idx> {};
template <size_t _Idx>
struct __overload<bool const volatile, _Idx> : __overload_bool<bool const volatile, _Idx> {};

template <class ..._Bases>
struct __all_overloads : _Bases... {
void operator()() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string>
#include <type_traits>
#include <variant>
#include <vector>
#include <memory>

#include "test_macros.h"
Expand Down Expand Up @@ -145,8 +146,8 @@ void test_T_assignment_sfinae() {
};
static_assert(!std::is_assignable<V, X>::value,
"no boolean conversion in operator=");
static_assert(!std::is_assignable<V, std::false_type>::value,
"no converted to bool in operator=");
static_assert(std::is_assignable<V, std::false_type>::value,
"converted to bool in operator=");
}
{
struct X {};
Expand Down Expand Up @@ -295,12 +296,21 @@ void test_T_assignment_performs_assignment() {
#endif // TEST_HAS_NO_EXCEPTIONS
}

void test_T_assignment_vector_bool() {
std::vector<bool> vec = {true};
std::variant<bool, int> v;
v = vec[0];
assert(v.index() == 0);
assert(std::get<0>(v) == true);
}

int main(int, char**) {
test_T_assignment_basic();
test_T_assignment_performs_construction();
test_T_assignment_performs_assignment();
test_T_assignment_noexcept();
test_T_assignment_sfinae();
test_T_assignment_vector_bool();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ int main(int, char**)

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

static_assert(!std::is_assignable<std::variant<bool>, std::true_type>::value, "");
static_assert(std::is_assignable<std::variant<bool>, std::true_type>::value, "");
static_assert(!std::is_assignable<std::variant<bool>, std::unique_ptr<char> >::value, "");
static_assert(!std::is_assignable<std::variant<bool>, decltype(nullptr)>::value, "");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <type_traits>
#include <variant>
#include <memory>
#include <vector>

#include "test_macros.h"
#include "variant_test_helpers.h"
Expand Down Expand Up @@ -79,8 +80,8 @@ void test_T_ctor_sfinae() {
};
static_assert(!std::is_constructible<V, X>::value,
"no boolean conversion in constructor");
static_assert(!std::is_constructible<V, std::false_type>::value,
"no converted to bool in constructor");
static_assert(std::is_constructible<V, std::false_type>::value,
"converted to bool in constructor");
}
{
struct X {};
Expand Down Expand Up @@ -138,12 +139,12 @@ void test_T_ctor_basic() {
assert(std::get<0>(v) == "foo");
}
{
std::variant<bool volatile, std::unique_ptr<int>> v = nullptr;
std::variant<bool, std::unique_ptr<int>> v = nullptr;
assert(v.index() == 1);
assert(std::get<1>(v) == nullptr);
}
{
std::variant<bool volatile const, int> v = true;
std::variant<bool const, int> v = true;
assert(v.index() == 0);
assert(std::get<0>(v));
}
Expand Down Expand Up @@ -198,11 +199,19 @@ void test_construction_with_repeated_types() {
static_assert(std::is_constructible<V, Bar>::value, "");
}

void test_vector_bool() {
std::vector<bool> vec = {true};
std::variant<bool, int> v = vec[0];
assert(v.index() == 0);
assert(std::get<0>(v) == true);
}

int main(int, char**) {
test_T_ctor_basic();
test_T_ctor_noexcept();
test_T_ctor_sfinae();
test_no_narrowing_check_for_class_types();
test_construction_with_repeated_types();
test_vector_bool();
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ int main(int, char**)

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

static_assert(!std::is_constructible<std::variant<bool>, std::true_type>::value, "");
static_assert(std::is_constructible<std::variant<bool>, std::true_type>::value, "");
static_assert(!std::is_constructible<std::variant<bool>, std::unique_ptr<char> >::value, "");
static_assert(!std::is_constructible<std::variant<bool>, decltype(nullptr)>::value, "");

Expand Down

0 comments on commit 170810f

Please sign in to comment.