Commit 9189f559 by Tim Shen Committed by Tim Shen

variant (std::get, operator==): Implement constexpr comparison and get<>.

	* include/std/variant (std::get, operator==): Implement constexpr
	comparison and get<>.
	* testsuite/20_util/variant/compile.cc: Tests.

From-SVN: r243293
parent 44f46885
2016-12-07 Tim Shen <timshen@google.com> 2016-12-07 Tim Shen <timshen@google.com>
* include/std/variant (std::get, operator==): Implement constexpr
comparison and get<>.
* testsuite/20_util/variant/compile.cc: Tests.
2016-12-07 Tim Shen <timshen@google.com>
* include/std/variant (__erased_use_alloc_ctor, * include/std/variant (__erased_use_alloc_ctor,
_Variant_base::_Variant_base, variant::variant): Remove uses-allocator _Variant_base::_Variant_base, variant::variant): Remove uses-allocator
related functions. related functions.
......
...@@ -51,6 +51,14 @@ struct DefaultNoexcept ...@@ -51,6 +51,14 @@ struct DefaultNoexcept
DefaultNoexcept& operator=(DefaultNoexcept&&) noexcept = default; DefaultNoexcept& operator=(DefaultNoexcept&&) noexcept = default;
}; };
struct nonliteral
{
nonliteral() { }
bool operator<(const nonliteral&) const;
bool operator==(const nonliteral&) const;
};
void default_ctor() void default_ctor()
{ {
static_assert(is_default_constructible_v<variant<int, string>>, ""); static_assert(is_default_constructible_v<variant<int, string>>, "");
...@@ -175,22 +183,40 @@ void test_get() ...@@ -175,22 +183,40 @@ void test_get()
void test_relational() void test_relational()
{ {
{ {
const variant<int, string> a, b; constexpr variant<int, nonliteral> a(42), b(43);
(void)(a < b); static_assert((a < b), "");
(void)(a > b); static_assert(!(a > b), "");
(void)(a <= b); static_assert((a <= b), "");
(void)(a == b); static_assert(!(a == b), "");
(void)(a != b); static_assert((a != b), "");
(void)(a >= b); static_assert(!(a >= b), "");
} }
{ {
const monostate a, b; constexpr variant<int, nonliteral> a(42), b(42);
(void)(a < b); static_assert(!(a < b), "");
(void)(a > b); static_assert(!(a > b), "");
(void)(a <= b); static_assert((a <= b), "");
(void)(a == b); static_assert((a == b), "");
(void)(a != b); static_assert(!(a != b), "");
(void)(a >= b); static_assert((a >= b), "");
}
{
constexpr variant<int, nonliteral> a(43), b(42);
static_assert(!(a < b), "");
static_assert((a > b), "");
static_assert(!(a <= b), "");
static_assert(!(a == b), "");
static_assert((a != b), "");
static_assert((a >= b), "");
}
{
constexpr monostate a, b;
static_assert(!(a < b), "");
static_assert(!(a > b), "");
static_assert((a <= b), "");
static_assert((a == b), "");
static_assert(!(a != b), "");
static_assert((a >= b), "");
} }
} }
...@@ -262,14 +288,59 @@ void test_constexpr() ...@@ -262,14 +288,59 @@ void test_constexpr()
constexpr literal() = default; constexpr literal() = default;
}; };
struct nonliteral {
nonliteral() { }
};
constexpr variant<literal, nonliteral> v{}; constexpr variant<literal, nonliteral> v{};
constexpr variant<literal, nonliteral> v1{in_place_type<literal>}; constexpr variant<literal, nonliteral> v1{in_place_type<literal>};
constexpr variant<literal, nonliteral> v2{in_place_index<0>}; constexpr variant<literal, nonliteral> v2{in_place_index<0>};
} }
{
constexpr variant<int> a(42);
static_assert(get<0>(a) == 42, "");
}
{
constexpr variant<int, nonliteral> a(42);
static_assert(get<0>(a) == 42, "");
}
{
constexpr variant<nonliteral, int> a(42);
static_assert(get<1>(a) == 42, "");
}
{
constexpr variant<int> a(42);
static_assert(get<int>(a) == 42, "");
}
{
constexpr variant<int, nonliteral> a(42);
static_assert(get<int>(a) == 42, "");
}
{
constexpr variant<nonliteral, int> a(42);
static_assert(get<int>(a) == 42, "");
}
{
constexpr variant<int> a(42);
static_assert(get<0>(std::move(a)) == 42, "");
}
{
constexpr variant<int, nonliteral> a(42);
static_assert(get<0>(std::move(a)) == 42, "");
}
{
constexpr variant<nonliteral, int> a(42);
static_assert(get<1>(std::move(a)) == 42, "");
}
{
constexpr variant<int> a(42);
static_assert(get<int>(std::move(a)) == 42, "");
}
{
constexpr variant<int, nonliteral> a(42);
static_assert(get<int>(std::move(a)) == 42, "");
}
{
constexpr variant<nonliteral, int> a(42);
static_assert(get<int>(std::move(a)) == 42, "");
}
} }
void test_pr77641() void test_pr77641()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment