Skip to content

Commit 72e1df8

Browse files
committed
[FIX] gcc11,12
1 parent b4b9445 commit 72e1df8

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

include/sharg/detail/poison_config.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,29 @@ concept poison_config_valid = (sizeof(poison_config) == sizeof(config<validator_
8989

9090
static_assert(poison_config_valid<>, "sharg::detail::poison_config must have the same members as sharg::config!");
9191

92+
/*!\brief This is a workaround for compilers that do not implement CWG2518 (GCC 11, GCC 12).
93+
* \ingroup parser
94+
* \sa https://en.cppreference.com/w/cpp/language/if#Constexpr_if
95+
* \sa https://cplusplus.github.io/CWG/issues/2518.html
96+
* \details
97+
* Before CWG2518, a (discarded) statement couldn't be false in every case, e.g.
98+
* ```cpp
99+
* template <typename option_type>
100+
* void add_option(option_type)
101+
* {
102+
* if constexpr (std::is_same_v<option_type, int>)
103+
* {
104+
* return;
105+
* }
106+
* else
107+
* {
108+
* static_assert(false, "Should never happen"); // invalid before CWG2518
109+
* static_assert(dependent_false_v<option_type>, "Should never happen"); // valid
110+
* }
111+
* }
112+
* ```
113+
*/
114+
template <typename>
115+
inline constexpr bool dependent_false_v = false;
116+
92117
} // namespace sharg::detail

include/sharg/parser.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class parser
263263
template <typename option_type>
264264
void add_option(option_type &, detail::poison_config const &)
265265
{
266-
static_assert(false, "Forgot sharg::config?");
266+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
267267
}
268268
//!\endcond
269269

@@ -299,10 +299,9 @@ class parser
299299
//!\cond DEV
300300
//!\brief A poison overload that catches calls to add_flag without explicitly passing a sharg::config.
301301
template <typename option_type> // Template needed to prevent instantiation of this function if unused.
302-
requires std::same_as<option_type, bool>
303302
void add_flag(option_type &, detail::poison_config const &)
304303
{
305-
static_assert(false, "Forgot sharg::config?");
304+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
306305
}
307306
//!\endcond
308307

@@ -353,7 +352,7 @@ class parser
353352
template <typename option_type>
354353
void add_positional_option(option_type &, detail::poison_config const &)
355354
{
356-
static_assert(false, "Forgot sharg::config?");
355+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
357356
}
358357
//!\endcond
359358

0 commit comments

Comments
 (0)