P1957R2: remove special-case for booleans in std::variant (#71502)
This picks up the std::variant half of P1957R2. Fixes #62332.
This commit is contained in:
@@ -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|"
|
||||
|
||||
|
@@ -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;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "test_macros.h"
|
||||
@@ -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 {};
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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, "");
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "test_macros.h"
|
||||
#include "variant_test_helpers.h"
|
||||
@@ -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 {};
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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, "");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user