Skip to content

Commit 26a094b

Browse files
committed
[FIX] gcc11,12
1 parent b4b9445 commit 26a094b

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,10 @@ class parser
261261
//!\cond DEV
262262
//!\brief A poison overload that catches calls to add_option without explicitly passing a sharg::config.
263263
template <typename option_type>
264+
requires (parsable<option_type> || parsable<std::ranges::range_value_t<option_type>>)
264265
void add_option(option_type &, detail::poison_config const &)
265266
{
266-
static_assert(false, "Forgot sharg::config?");
267+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
267268
}
268269
//!\endcond
269270

@@ -302,7 +303,7 @@ class parser
302303
requires std::same_as<option_type, bool>
303304
void add_flag(option_type &, detail::poison_config const &)
304305
{
305-
static_assert(false, "Forgot sharg::config?");
306+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
306307
}
307308
//!\endcond
308309

@@ -351,9 +352,10 @@ class parser
351352
//!\cond DEV
352353
//!\brief A poison overload that catches calls to add_positional_option without explicitly passing a sharg::config.
353354
template <typename option_type>
355+
requires (parsable<option_type> || parsable<std::ranges::range_value_t<option_type>>)
354356
void add_positional_option(option_type &, detail::poison_config const &)
355357
{
356-
static_assert(false, "Forgot sharg::config?");
358+
static_assert(detail::dependent_false_v<option_type>, "Forgot sharg::config?");
357359
}
358360
//!\endcond
359361

@@ -1011,3 +1013,5 @@ class parser
10111013
};
10121014

10131015
} // namespace sharg
1016+
1017+

0 commit comments

Comments
 (0)