Compare commits

...

5 Commits

Author SHA1 Message Date
Peter Dimov
4959611e09 Avoid msvc-14.0 constexpr errors in enum_to_string 2025-12-15 19:39:46 +02:00
Peter Dimov
a722380b2a Update revision history 2025-12-15 13:16:33 +02:00
Peter Dimov
dfd9182bf3 Replace the lambda in enum_to_string with a struct to enable C++14 constexpr. Refs #60. Refs #61. 2025-12-15 12:37:40 +02:00
Peter Dimov
f1774f3d02 Make enum_to_string constexpr; add enum_to_string_test_cx.cpp. Refs #60. Refs #61. 2025-12-15 12:28:44 +02:00
Peter Dimov
7c0d234267 Update ci.yml 2025-12-11 15:49:41 +02:00
5 changed files with 86 additions and 12 deletions

View File

@@ -67,6 +67,11 @@ jobs:
container: ubuntu:24.04
os: ubuntu-latest
install: g++-14
- toolset: gcc-15
cxxstd: "03,11,14,17,20,23,2c"
container: ubuntu:25.10
os: ubuntu-latest
install: g++-15
- toolset: clang
compiler: clang++-3.9
cxxstd: "03,11,14"
@@ -170,14 +175,26 @@ jobs:
os: ubuntu-latest
install: clang-19
- toolset: clang
cxxstd: "03,11,14,17,20,2b"
os: macos-13
compiler: clang++-20
cxxstd: "03,11,14,17,20,23,2c"
container: ubuntu:24.04
os: ubuntu-latest
install: clang-20
- toolset: clang
compiler: clang++-21
cxxstd: "03,11,14,17,20,23,2c"
container: ubuntu:25.10
os: ubuntu-latest
install: clang-21
- toolset: clang
cxxstd: "03,11,14,17,20,2b"
os: macos-14
- toolset: clang
cxxstd: "03,11,14,17,20,2b"
os: macos-15
- toolset: clang
cxxstd: "03,11,14,17,20,23,2c"
os: macos-26
runs-on: ${{matrix.os}}
@@ -310,9 +327,9 @@ jobs:
include:
- os: ubuntu-22.04
- os: ubuntu-24.04
- os: macos-13
- os: macos-14
- os: macos-15
- os: macos-26
runs-on: ${{matrix.os}}
@@ -358,9 +375,9 @@ jobs:
include:
- os: ubuntu-22.04
- os: ubuntu-24.04
- os: macos-13
- os: macos-14
- os: macos-15
- os: macos-26
runs-on: ${{matrix.os}}
@@ -416,9 +433,9 @@ jobs:
include:
- os: ubuntu-22.04
- os: ubuntu-24.04
- os: macos-13
- os: macos-14
- os: macos-15
- os: macos-26
runs-on: ${{matrix.os}}

View File

@@ -8,6 +8,11 @@ https://www.boost.org/LICENSE_1_0.txt
# Revision History
:idprefix:
## Changes in Boost 1.91.0
* Under {cpp}20, described nested enums now work when the enclosing class is still incomplete. (Thanks to Julien Blanc.)
* Made `enum_to_string` constexpr. (Thanks to Julien Blanc.)
## Changes in Boost 1.84.0
* Added an overload of `enum_from_string` that takes a string view, to avoid

View File

@@ -1,7 +1,7 @@
#ifndef BOOST_DESCRIBE_ENUM_TO_STRING_HPP_INCLUDED
#define BOOST_DESCRIBE_ENUM_TO_STRING_HPP_INCLUDED
// Copyright 2020, 2021 Peter Dimov
// Copyright 2020, 2021, 2025 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
@@ -22,16 +22,34 @@ namespace boost
namespace describe
{
template<class E, class De = describe_enumerators<E>>
char const * enum_to_string( E e, char const* def ) noexcept
namespace detail
{
char const * r = def;
mp11::mp_for_each<De>([&](auto D){
// [&](auto D){ if( e == D.value ) r = D.name; }
// except constexpr under C++14
if( e == D.value ) r = D.name;
template<class E> struct ets_lambda
{
E e;
char const** pr;
});
template<class D> constexpr void operator()( D d ) const noexcept
{
if( e == d.value ) *pr = d.name;
}
};
} // namespace detail
template<class E, class De = describe_enumerators<E>>
#if !( defined(_MSC_VER) && _MSC_VER == 1900 )
constexpr
#endif
char const* enum_to_string( E e, char const* def ) noexcept
{
char const* r = def;
mp11::mp_for_each<De>( detail::ets_lambda<E>{ e, &r } );
return r;
}

View File

@@ -87,6 +87,8 @@ run enum_from_string_test2.cpp ;
compile nested_enum_test2.cpp ;
compile enum_to_string_test_cx.cpp ;
# examples
obj describe_cxx14 : describe_cxx14.cpp ;

View File

@@ -0,0 +1,32 @@
// Copyright 2021, 2025 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/describe/enum_to_string.hpp>
#include <boost/describe/enum.hpp>
#include <boost/describe/detail/cx_streq.hpp>
#include <boost/config/pragma_message.hpp>
#if !defined(BOOST_DESCRIBE_CXX14)
BOOST_PRAGMA_MESSAGE("Skipping test because C++14 is not available")
#elif defined(_MSC_VER) && _MSC_VER == 1900
BOOST_PRAGMA_MESSAGE("Skipping test because _MSC_VER == 1900")
#else
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
BOOST_DEFINE_ENUM_CLASS(E, v1, v2, v3)
using boost::describe::enum_to_string;
using boost::describe::detail::cx_streq;
STATIC_ASSERT( cx_streq( enum_to_string( E::v1, "" ), "v1" ) );
STATIC_ASSERT( cx_streq( enum_to_string( E::v2, "" ), "v2" ) );
STATIC_ASSERT( cx_streq( enum_to_string( E::v3, "" ), "v3" ) );
STATIC_ASSERT( cx_streq( enum_to_string( (E)17, "def" ), "def" ) );
#endif // !defined(BOOST_DESCRIBE_CXX14)