Skip to content

Commit

Permalink
Support BOOST_ASSERT in C++11 constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
glenfe committed Jan 17, 2025
1 parent fa72d2f commit da76310
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
13 changes: 11 additions & 2 deletions include/boost/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Copyright (c) 2007, 2014 Peter Dimov
// Copyright (c) Beman Dawes 2011
// Copyright (c) 2015 Ion Gaztanaga
// Copyright (c) 2025 Glen Joseph Fernandes ([email protected])
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -29,6 +30,7 @@
// BOOST_ASSERT, BOOST_ASSERT_MSG, BOOST_ASSERT_IS_VOID
//

#undef BOOST_CASSERT
#undef BOOST_ASSERT
#undef BOOST_ASSERT_MSG
#undef BOOST_ASSERT_IS_VOID
Expand Down Expand Up @@ -61,10 +63,17 @@ namespace boost

#else

# include <boost/config.hpp>
# include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same

# define BOOST_ASSERT(expr) assert(expr)
# define BOOST_ASSERT_MSG(expr, msg) assert((expr)&&(msg))
#if defined(BOOST_GCC) && BOOST_GCC < 50000
# define BOOST_CASSERT(expr) (BOOST_LIKELY(!!(expr))? ((void)0): __assert_fail (#expr, __FILE__, __LINE__, ""))
#else
# define BOOST_CASSERT(expr) assert(expr)
#endif

# define BOOST_ASSERT(expr) BOOST_CASSERT(expr)
# define BOOST_ASSERT_MSG(expr, msg) BOOST_CASSERT((expr)&&(msg))
#if defined(NDEBUG)
# define BOOST_ASSERT_IS_VOID
#endif
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ run exp/verify_exp_test.cpp ;
run exp/verify_msg_exp_test.cpp ;
run assert_test2.cpp ;
run assert_msg_test2.cpp ;
compile assert_constexpr_test.cpp ;

# quick test (for CI)
run quick.cpp ;
Expand Down
35 changes: 35 additions & 0 deletions test/assert_constexpr_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2025 Glen Joseph Fernandes
([email protected])
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_CONSTEXPR)
#include <boost/core/lightweight_test.hpp>
#undef NDEBUG
#include <boost/assert.hpp>

constexpr int subtract(int a, int b)
{
return BOOST_ASSERT(a > b), a - b;
}

constexpr int divide(int a, int b)
{
return BOOST_ASSERT_MSG(a > b, "text"), a / b;
}

void test_assert()
{
constexpr int i = subtract(5, 1);
(void)i;
}

void test_assert_msg()
{
constexpr int i = divide(5, 1);
(void)i;
}
#endif

0 comments on commit da76310

Please sign in to comment.