Skip to content

Commit 0d94776

Browse files
committed
Use C++26 placeholder variables in BOOST_SCOPE_DEFER.
This allows for avoiding relying on macros for generating unique variable names.
1 parent efeb557 commit 0d94776

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

include/boost/scope/defer.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* (See accompanying file LICENSE_1_0.txt or copy at
44
* https://www.boost.org/LICENSE_1_0.txt)
55
*
6-
* Copyright (c) 2022-2024 Andrey Semashev
6+
* Copyright (c) 2022-2026 Andrey Semashev
77
*/
88
/*!
99
* \file scope/defer.hpp
@@ -148,11 +148,18 @@ defer_guard(Func) -> defer_guard< Func >;
148148
} // namespace scope
149149

150150
//! \cond
151+
// Clang 18 and later eagerly defines __cpp_placeholder_variables but then emits warnings unless C++26 is enabled:
152+
// https://github.com/llvm/llvm-project/issues/216035
153+
#if defined(__cpp_placeholder_variables) && (__cpp_placeholder_variables >= 202306l) && (!defined(BOOST_CLANG) || (BOOST_CXX_VERSION >= 202603l))
154+
#define BOOST_SCOPE_DETAIL_PLACEHOLDER_VAR_NAME _
155+
#else // defined(__cpp_placeholder_variables)...
151156
#if defined(BOOST_MSVC)
152157
#define BOOST_SCOPE_DETAIL_UNIQUE_VAR_TAG __COUNTER__
153158
#else
154159
#define BOOST_SCOPE_DETAIL_UNIQUE_VAR_TAG __LINE__
155160
#endif
161+
#define BOOST_SCOPE_DETAIL_PLACEHOLDER_VAR_NAME BOOST_JOIN(_boost_scope_defer_guard_, BOOST_SCOPE_DETAIL_UNIQUE_VAR_TAG)
162+
#endif // defined(__cpp_placeholder_variables)...
156163
//! \endcond
157164

158165
/*!
@@ -170,8 +177,7 @@ defer_guard(Func) -> defer_guard< Func >;
170177
*
171178
* \note Using this macro requires C++17.
172179
*/
173-
#define BOOST_SCOPE_DEFER \
174-
boost::scope::defer_guard BOOST_JOIN(_boost_defer_guard_, BOOST_SCOPE_DETAIL_UNIQUE_VAR_TAG) =
180+
#define BOOST_SCOPE_DEFER boost::scope::defer_guard BOOST_SCOPE_DETAIL_PLACEHOLDER_VAR_NAME =
175181

176182
} // namespace boost
177183

0 commit comments

Comments
 (0)