| 1 | // Copyright (c) 2016-2024 Antony Polukhin |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include <boost/pfr/ops.hpp> |
| 7 | #include <boost/pfr/io.hpp> |
| 8 | |
| 9 | #include <iostream> |
| 10 | #include <typeinfo> |
| 11 | #include <tuple> |
| 12 | #include <sstream> |
| 13 | #include <set> |
| 14 | #include <string> |
| 15 | |
| 16 | #include <boost/config.hpp> |
| 17 | #include <boost/core/lightweight_test.hpp> |
| 18 | |
| 19 | #ifdef __clang__ |
| 20 | # pragma clang diagnostic ignored "-Wmissing-braces" |
| 21 | #endif |
| 22 | |
| 23 | union test_union { |
| 24 | int i; |
| 25 | float f; |
| 26 | }; |
| 27 | |
| 28 | constexpr bool operator< (test_union l, test_union r) noexcept { return l.i < r.i; } |
| 29 | constexpr bool operator<=(test_union l, test_union r) noexcept { return l.i <= r.i; } |
| 30 | constexpr bool operator> (test_union l, test_union r) noexcept { return l.i > r.i; } |
| 31 | constexpr bool operator>=(test_union l, test_union r) noexcept { return l.i >= r.i; } |
| 32 | constexpr bool operator==(test_union l, test_union r) noexcept { return l.i == r.i; } |
| 33 | constexpr bool operator!=(test_union l, test_union r) noexcept { return l.i != r.i; } |
| 34 | |
| 35 | |
| 36 | template <class T> |
| 37 | void test_constexpr_comparable() { |
| 38 | using namespace boost::pfr; |
| 39 | constexpr T s1 {110, 1, true, 6,17,8,9,10,11}; |
| 40 | constexpr T s2 = s1; |
| 41 | constexpr T s3 {110, 1, true, 6,17,8,9,10,11111}; |
| 42 | static_assert(eq(s1, s2), "" ); |
| 43 | static_assert(le(s1, s2), "" ); |
| 44 | static_assert(ge(s1, s2), "" ); |
| 45 | static_assert(!ne(s1, s2), "" ); |
| 46 | static_assert(!eq(s1, s3), "" ); |
| 47 | static_assert(ne(s1, s3), "" ); |
| 48 | static_assert(lt(s1, s3), "" ); |
| 49 | static_assert(gt(s3, s2), "" ); |
| 50 | static_assert(le(s1, s3), "" ); |
| 51 | static_assert(ge(s3, s2), "" ); |
| 52 | } |
| 53 | |
| 54 | namespace foo { |
| 55 | struct comparable_struct { |
| 56 | int i; short s; bool bl; int a,b,c,d,e,f; |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | int main() { |
| 61 | // MSVC fails to use strucutred bindings in constexpr: |
| 62 | // |
| 63 | // error C2131: expression did not evaluate to a constant |
| 64 | // pfr/detail/functional.hpp(21): note: failure was caused by a read of a variable outside its lifetime |
| 65 | #if !defined(_MSC_VER) || (_MSC_VER >= 1927) || !BOOST_PFR_USE_CPP17 |
| 66 | test_constexpr_comparable<foo::comparable_struct>(); |
| 67 | |
| 68 | struct local_comparable_struct { |
| 69 | int i; short s; bool bl; int a,b,c,d,e,f; |
| 70 | }; |
| 71 | test_constexpr_comparable<local_comparable_struct>(); |
| 72 | |
| 73 | struct local_comparable_struct_with_union { |
| 74 | int i; short s; bool bl; int a,b,c,d,e; test_union u; |
| 75 | }; |
| 76 | test_constexpr_comparable<local_comparable_struct>(); |
| 77 | #endif |
| 78 | return boost::report_errors(); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |