constexpr
address_v4(const bytes_type& __b)
- : _M_addr((__b[0] << 24) | (__b[1] << 16) | (__b[2] << 8) | __b[3])
+#if __has_builtin(__builtin_bit_cast)
+ : _M_addr(__builtin_bit_cast(uint_type, __b))
+#else
+ : _M_addr(_S_hton_32((__b[0] << 24) | (__b[1] << 16)
+ | (__b[2] << 8) | __b[3]))
+#endif
{ }
explicit constexpr
constexpr bytes_type
to_bytes() const noexcept
{
+#if __has_builtin(__builtin_bit_cast)
+ return __builtin_bit_cast(bytes_type, _M_addr);
+#else
+ auto __host = to_uint();
return bytes_type{
- (_M_addr >> 24) & 0xFF,
- (_M_addr >> 16) & 0xFF,
- (_M_addr >> 8) & 0xFF,
- _M_addr & 0xFF
+ (__host >> 24) & 0xFF,
+ (__host >> 16) & 0xFF,
+ (__host >> 8) & 0xFF,
+ __host & 0xFF
};
+#endif
}
constexpr uint_type
using std::experimental::net::ip::address_v4;
-void
-test01()
+#if __cplusplus < 202002L
+// Naughty, but operator== for std::array is not constexpr until C++20.
+constexpr bool
+operator==(const address_v4::bytes_type& lhs, const address_v4::bytes_type& rhs)
{
- bool test __attribute__((unused)) = false;
+ return lhs[0] == rhs[0] && lhs[1] == rhs[1]
+ && lhs[2] == rhs[2] && lhs[3] == rhs[3];
+}
+#endif
+constexpr void
+test01()
+{
address_v4 a0;
VERIFY( a0.to_uint() == 0 );
VERIFY( a0.to_bytes() == address_v4::bytes_type{} );
}
-void
+constexpr void
test02()
{
- bool test __attribute__((unused)) = false;
-
address_v4 a0{ address_v4::bytes_type{} };
VERIFY( a0.to_uint() == 0 );
VERIFY( a0.to_bytes() == address_v4::bytes_type{} );
address_v4::bytes_type b1{ 1, 2, 3, 4 };
address_v4 a1{ b1 };
- VERIFY( a1.to_uint() == ntohl((1 << 24) | (2 << 16) | (3 << 8) | 4) );
+ VERIFY( a1.to_uint() == ((1 << 24) | (2 << 16) | (3 << 8) | 4) );
VERIFY( a1.to_bytes() == b1 );
}
-void
+constexpr void
test03()
{
- bool test __attribute__((unused)) = false;
-
address_v4 a0{ 0u };
VERIFY( a0.to_uint() == 0 );
VERIFY( a0.to_bytes() == address_v4::bytes_type{} );
- address_v4::uint_type u1 = ntohl((5 << 24) | (6 << 16) | (7 << 8) | 8);
+ address_v4::uint_type u1 = (5 << 24) | (6 << 16) | (7 << 8) | 8;
address_v4 a1{ u1 };
VERIFY( a1.to_uint() == u1 );
VERIFY( a1.to_bytes() == address_v4::bytes_type( 5, 6, 7, 8 ) );
test01();
test02();
test03();
+
+ constexpr bool c = []{
+ test01();
+ test02();
+ test03();
+ return true;
+ };
}
namespace net = std::experimental::net;
using net::ip::address_v4;
-void
+#if __cplusplus < 202002L
+// Naughty, but operator== for std::array is not constexpr until C++20.
+constexpr bool
+operator==(const address_v4::bytes_type& lhs, const address_v4::bytes_type& rhs)
+{
+ return lhs[0] == rhs[0] && lhs[1] == rhs[1]
+ && lhs[2] == rhs[2] && lhs[3] == rhs[3];
+}
+#endif
+
+constexpr void
test01()
{
auto a0 = make_address_v4( address_v4::bytes_type{} );
address_v4::bytes_type b1{ 1, 2, 3, 4 };
auto a1 = make_address_v4( b1 );
- VERIFY( a1.to_uint() == ntohl((1 << 24) | (2 << 16) | (3 << 8) | 4) );
+ VERIFY( a1.to_uint() == ((1 << 24) | (2 << 16) | (3 << 8) | 4) );
VERIFY( a1.to_bytes() == b1 );
}
-void
+constexpr void
test02()
{
auto a0 = net::ip::make_address_v4(0u);
VERIFY( a0.to_uint() == 0 );
VERIFY( a0.to_bytes() == address_v4::bytes_type{} );
- address_v4::uint_type u1 = ntohl((5 << 24) | (6 << 16) | (7 << 8) | 8);
+ address_v4::uint_type u1 = ((5 << 24) | (6 << 16) | (7 << 8) | 8);
auto a1 = net::ip::make_address_v4( u1 );
VERIFY( a1.to_uint() == u1 );
VERIFY( a1.to_bytes() == address_v4::bytes_type( 5, 6, 7, 8 ) );
test01();
test02();
test03();
+
+ constexpr bool c = []{
+ test01();
+ test02();
+ return true;
+ };
}
using std::experimental::net::ip::address_v4;
+static_assert(std::is_standard_layout<address_v4::bytes_type>::value,
+ "net::ip::address_v4::bytes_type is a standard layout type");
+
constexpr bool
test01()
{