Skip to content

Commit af53f17

Browse files
committed
Add constexpr to virtual functions on C++20 or later. Refs #141.
1 parent 6598312 commit af53f17

File tree

6 files changed

+88
-18
lines changed

6 files changed

+88
-18
lines changed

include/boost/system/detail/config.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
# define BOOST_SYSTEM_CONSTEXPR
3939
#endif
4040

41+
// BOOST_SYSTEM_HAS_CXX20_CONSTEXPR
42+
43+
#if defined(__cpp_constexpr) && __cpp_constexpr >= 201907L
44+
# define BOOST_SYSTEM_HAS_CXX20_CONSTEXPR
45+
#endif
46+
47+
#if defined(BOOST_SYSTEM_HAS_CXX20_CONSTEXPR)
48+
# define BOOST_SYSTEM_CXX20_CONSTEXPR constexpr
49+
#else
50+
# define BOOST_SYSTEM_CXX20_CONSTEXPR
51+
#endif
52+
4153
// BOOST_SYSTEM_DEPRECATED
4254

4355
#if defined(__clang__)

include/boost/system/detail/error_category.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,31 @@ class BOOST_SYMBOL_VISIBLE error_category
9494

9595
public:
9696

97-
virtual const char * name() const noexcept = 0;
97+
virtual const char* name() const noexcept = 0;
9898

99-
virtual error_condition default_error_condition( int ev ) const noexcept;
100-
virtual bool equivalent( int code, const error_condition & condition ) const noexcept;
101-
virtual bool equivalent( const error_code & code, int condition ) const noexcept;
99+
BOOST_SYSTEM_CXX20_CONSTEXPR virtual error_condition default_error_condition( int ev ) const noexcept;
100+
BOOST_SYSTEM_CXX20_CONSTEXPR virtual bool equivalent( int code, error_condition const& condition ) const noexcept;
101+
BOOST_SYSTEM_CXX20_CONSTEXPR virtual bool equivalent( error_code const& code, int condition ) const noexcept;
102102

103103
virtual std::string message( int ev ) const = 0;
104-
virtual char const * message( int ev, char * buffer, std::size_t len ) const noexcept;
104+
virtual char const* message( int ev, char* buffer, std::size_t len ) const noexcept;
105105

106-
virtual bool failed( int ev ) const noexcept
106+
BOOST_SYSTEM_CXX20_CONSTEXPR virtual bool failed( int ev ) const noexcept
107107
{
108108
return ev != 0;
109109
}
110110

111-
friend BOOST_SYSTEM_CONSTEXPR bool operator==( error_category const & lhs, error_category const & rhs ) noexcept
111+
friend BOOST_SYSTEM_CONSTEXPR bool operator==( error_category const& lhs, error_category const& rhs ) noexcept
112112
{
113113
return rhs.id_ == 0? &lhs == &rhs: lhs.id_ == rhs.id_;
114114
}
115115

116-
friend BOOST_SYSTEM_CONSTEXPR bool operator!=( error_category const & lhs, error_category const & rhs ) noexcept
116+
friend BOOST_SYSTEM_CONSTEXPR bool operator!=( error_category const& lhs, error_category const& rhs ) noexcept
117117
{
118118
return !( lhs == rhs );
119119
}
120120

121-
friend BOOST_SYSTEM_CONSTEXPR bool operator<( error_category const & lhs, error_category const & rhs ) noexcept
121+
friend BOOST_SYSTEM_CONSTEXPR bool operator<( error_category const& lhs, error_category const& rhs ) noexcept
122122
{
123123
if( lhs.id_ < rhs.id_ )
124124
{
@@ -135,15 +135,15 @@ class BOOST_SYMBOL_VISIBLE error_category
135135
return false; // equal
136136
}
137137

138-
return std::less<error_category const *>()( &lhs, &rhs );
138+
return std::less<error_category const*>()( &lhs, &rhs );
139139
}
140140

141141
void init_stdcat() const;
142142

143143
# if defined(__SUNPRO_CC) // trailing __global is not supported
144-
operator std::error_category const & () const;
144+
operator std::error_category const& () const;
145145
# else
146-
operator std::error_category const & () const BOOST_SYMBOL_VISIBLE;
146+
operator std::error_category const& () const BOOST_SYMBOL_VISIBLE;
147147
# endif
148148
};
149149

@@ -162,7 +162,7 @@ static const boost::ulong_long_type generic_category_id = ( boost::ulong_long_ty
162162
static const boost::ulong_long_type system_category_id = generic_category_id + 1;
163163
static const boost::ulong_long_type interop_category_id = generic_category_id + 2;
164164

165-
BOOST_SYSTEM_CONSTEXPR inline bool failed_impl( int ev, error_category const & cat )
165+
BOOST_SYSTEM_CONSTEXPR inline bool failed_impl( int ev, error_category const& cat )
166166
{
167167
if( cat.id_ == system_category_id || cat.id_ == generic_category_id )
168168
{

include/boost/system/detail/error_category_impl.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ namespace system
2626

2727
// error_category default implementation
2828

29-
inline error_condition error_category::default_error_condition( int ev ) const noexcept
29+
BOOST_SYSTEM_CXX20_CONSTEXPR inline error_condition error_category::default_error_condition( int ev ) const noexcept
3030
{
3131
return error_condition( ev, *this );
3232
}
3333

34-
inline bool error_category::equivalent( int code, const error_condition & condition ) const noexcept
34+
BOOST_SYSTEM_CXX20_CONSTEXPR inline bool error_category::equivalent( int code, error_condition const& condition ) const noexcept
3535
{
3636
return default_error_condition( code ) == condition;
3737
}
3838

39-
inline bool error_category::equivalent( const error_code & code, int condition ) const noexcept
39+
BOOST_SYSTEM_CXX20_CONSTEXPR inline bool error_category::equivalent( error_code const& code, int condition ) const noexcept
4040
{
4141
return code.equals( condition, *this );
4242
}
4343

44-
inline char const * error_category::message( int ev, char * buffer, std::size_t len ) const noexcept
44+
inline char const* error_category::message( int ev, char* buffer, std::size_t len ) const noexcept
4545
{
4646
if( len == 0 )
4747
{
@@ -116,7 +116,7 @@ inline void error_category::init_stdcat() const
116116
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
117117
#endif
118118

119-
inline BOOST_NOINLINE error_category::operator std::error_category const & () const
119+
inline BOOST_NOINLINE error_category::operator std::error_category const& () const
120120
{
121121
if( id_ == detail::generic_category_id )
122122
{

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ boost_test(TYPE run SOURCES ec_hash_value_test.cpp)
135135

136136
boost_test(TYPE run SOURCES std_interop_test16.cpp)
137137

138+
boost_test(TYPE run SOURCES failed_constexpr_test2.cpp)
139+
138140
# result
139141

140142
boost_test(TYPE run SOURCES result_default_construct.cpp)

test/Jamfile.v2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ run ec_hash_value_test.cpp ;
168168

169169
run std_interop_test16.cpp ;
170170

171+
run failed_constexpr_test2.cpp ;
172+
171173
# result
172174

173175
run result_default_construct.cpp ;

test/failed_constexpr_test2.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2026 Peter Dimov
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
5+
#include <boost/system/error_code.hpp>
6+
#include <boost/system/error_condition.hpp>
7+
#include <boost/config/pragma_message.hpp>
8+
#include <boost/static_assert.hpp>
9+
#include <cstdio>
10+
11+
#if !defined(BOOST_SYSTEM_HAS_CXX20_CONSTEXPR)
12+
13+
BOOST_PRAGMA_MESSAGE("Skipping constexpr test, BOOST_SYSTEM_HAS_CXX20_CONSTEXPR isn't defined")
14+
int main() {}
15+
16+
#else
17+
18+
namespace sys = boost::system;
19+
20+
class user_category: public sys::error_category
21+
{
22+
public:
23+
24+
constexpr virtual const char* name() const noexcept
25+
{
26+
return "user";
27+
}
28+
29+
virtual std::string message( int ev ) const
30+
{
31+
char buffer[ 256 ];
32+
std::snprintf( buffer, sizeof( buffer ), "user message %d", ev );
33+
34+
return buffer;
35+
}
36+
};
37+
38+
static constexpr user_category s_user_cat;
39+
40+
constexpr sys::error_code ec( 1, s_user_cat );
41+
42+
BOOST_STATIC_ASSERT( ec.failed() );
43+
BOOST_STATIC_ASSERT( ec );
44+
BOOST_STATIC_ASSERT( !!ec );
45+
46+
constexpr sys::error_condition en( 1, s_user_cat );
47+
48+
BOOST_STATIC_ASSERT( en.failed() );
49+
BOOST_STATIC_ASSERT( en );
50+
BOOST_STATIC_ASSERT( !!en );
51+
52+
int main() {}
53+
54+
#endif

0 commit comments

Comments
 (0)