From c6d8a9a1f03f01d5c1e4c24765fa21dacaa5a6c8 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 8 Sep 2025 22:23:25 +0300 Subject: [PATCH] Make constants constexpr under C++14 --- include/boost/uuid/detail/nil_uuid.hpp | 4 +-- include/boost/uuid/namespaces.hpp | 21 ++++++-------- test/CMakeLists.txt | 1 + test/Jamfile.v2 | 1 + test/test_constants_cx.cpp | 39 ++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 test/test_constants_cx.cpp diff --git a/include/boost/uuid/detail/nil_uuid.hpp b/include/boost/uuid/detail/nil_uuid.hpp index 2371823..64124b1 100644 --- a/include/boost/uuid/detail/nil_uuid.hpp +++ b/include/boost/uuid/detail/nil_uuid.hpp @@ -10,9 +10,9 @@ namespace boost { namespace uuids { -inline uuid nil_uuid() noexcept +constexpr uuid nil_uuid() noexcept { - return {{}}; + return {}; } }} // namespace boost::uuids diff --git a/include/boost/uuid/namespaces.hpp b/include/boost/uuid/namespaces.hpp index 82c4ec7..7747e7a 100644 --- a/include/boost/uuid/namespaces.hpp +++ b/include/boost/uuid/namespaces.hpp @@ -7,41 +7,38 @@ // https://www.boost.org/LICENSE_1_0.txt #include +#include namespace boost { namespace uuids { namespace ns { -inline uuid dns() noexcept +BOOST_CXX14_CONSTEXPR inline uuid dns() noexcept { - uuid result = {{ + return {{ 0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }}; - return result; } -inline uuid url() noexcept +BOOST_CXX14_CONSTEXPR inline uuid url() noexcept { - uuid result = {{ + return {{ 0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }}; - return result; } -inline uuid oid() noexcept +BOOST_CXX14_CONSTEXPR inline uuid oid() noexcept { - uuid result = {{ + return {{ 0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }}; - return result; } -inline uuid x500dn() noexcept +BOOST_CXX14_CONSTEXPR inline uuid x500dn() noexcept { - uuid result = {{ + return {{ 0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 }}; - return result; } }}} // namespace boost::uuids::ns diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 996f73b..42104ae 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -85,5 +85,6 @@ boost_test(TYPE run SOURCES test_constants.cpp) boost_test(TYPE compile SOURCES test_uuid_cx.cpp) boost_test(TYPE run SOURCES test_string_generator_cx.cpp) +boost_test(TYPE run SOURCES test_constants_cx.cpp) boost_test(TYPE run SOURCES quick.cpp) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index dcad707..b09cb0d 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -211,6 +211,7 @@ run test_constants.cpp ; compile test_uuid_cx.cpp ; run test_string_generator_cx.cpp ; +run test_constants_cx.cpp ; # 'quick' test for CI diff --git a/test/test_constants_cx.cpp b/test/test_constants_cx.cpp new file mode 100644 index 0000000..23058eb --- /dev/null +++ b/test/test_constants_cx.cpp @@ -0,0 +1,39 @@ +// Copyright 2025 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include +#include +#include + +#if defined(BOOST_NO_CXX14_CONSTEXPR) + +BOOST_PRAGMA_MESSAGE("Skipping test because BOOST_NO_CXX14_CONSTEXPR is defined") +int main() {} + +#else + +using namespace boost::uuids; + +constexpr auto u1 = ns::dns(); +constexpr auto u2 = ns::url(); +constexpr auto u3 = ns::oid(); +constexpr auto u4 = ns::x500dn(); +constexpr auto u5 = nil_uuid(); + +int main() +{ + BOOST_TEST_EQ( u1, string_generator()("6ba7b810-9dad-11d1-80b4-00c04fd430c8")); + BOOST_TEST_EQ( u2, string_generator()("6ba7b811-9dad-11d1-80b4-00c04fd430c8")); + BOOST_TEST_EQ( u3, string_generator()("6ba7b812-9dad-11d1-80b4-00c04fd430c8")); + BOOST_TEST_EQ( u4, string_generator()("6ba7b814-9dad-11d1-80b4-00c04fd430c8")); + BOOST_TEST_EQ( u5, string_generator()("00000000-0000-0000-0000-000000000000")); + + return boost::report_errors(); +} + +#endif