Skip to content

Commit 3f190ab

Browse files
committed
[FIX] gcc11,12
1 parent 8003570 commit 3f190ab

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
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 & 3 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

@@ -301,7 +301,7 @@ class parser
301301
template <typename option_type> // Template needed to prevent instantiation of this function if unused.
302302
void add_flag(option_type &, detail::poison_config const &)
303303
{
304-
static_assert(false, "Forgot sharg::config?");
304+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
305305
}
306306
//!\endcond
307307

@@ -352,7 +352,7 @@ class parser
352352
template <typename option_type>
353353
void add_positional_option(option_type &, detail::poison_config const &)
354354
{
355-
static_assert(false, "Forgot sharg::config?");
355+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
356356
}
357357
//!\endcond
358358

0 commit comments

Comments
 (0)