Commit b73d1bd9b23abaa4d01f76578627c7f5cf83aaf9
1 parent
aae31265
Trying enable if, new version
Showing
1 changed file
with
24 additions
and
18 deletions
include/CLI.hpp
| ... | ... | @@ -36,6 +36,18 @@ std::string join(const T& v, std::string delim = ",") { |
| 36 | 36 | |
| 37 | 37 | namespace CLI { |
| 38 | 38 | |
| 39 | +// Based generally on https://rmf.io/cxx11/almost-static-if | |
| 40 | +namespace detail { | |
| 41 | + enum class enabler {}; | |
| 42 | +} | |
| 43 | +constexpr detail::enabler dummy = {}; | |
| 44 | + | |
| 45 | +template <typename Condition> | |
| 46 | +using EnableIf = typename std::enable_if<Condition::value, detail::enabler>::type; | |
| 47 | +template <typename Condition> | |
| 48 | +using DisableIf = typename std::enable_if<!Condition::value, detail::enabler>::type; | |
| 49 | + | |
| 50 | + | |
| 39 | 51 | struct Combiner { |
| 40 | 52 | int num; |
| 41 | 53 | bool positional; |
| ... | ... | @@ -252,9 +264,8 @@ public: |
| 252 | 264 | }; |
| 253 | 265 | |
| 254 | 266 | |
| 255 | -template<typename T> | |
| 256 | -typename std::enable_if<std::is_integral<T>::value, bool>::type | |
| 257 | -lexical_cast(std::string input, T& output) { | |
| 267 | +template<typename T, EnableIf<std::is_integral<T>> = dummy> | |
| 268 | +bool lexical_cast(std::string input, T& output) { | |
| 258 | 269 | logit("Int lexical cast " + input); |
| 259 | 270 | try{ |
| 260 | 271 | output = (T) std::stoll(input); |
| ... | ... | @@ -266,9 +277,8 @@ lexical_cast(std::string input, T& output) { |
| 266 | 277 | } |
| 267 | 278 | } |
| 268 | 279 | |
| 269 | -template<typename T> | |
| 270 | -typename std::enable_if<std::is_floating_point<T>::value, bool>::type | |
| 271 | -lexical_cast(std::string input, T& output) { | |
| 280 | +template<typename T, EnableIf<std::is_floating_point<T>> = dummy> | |
| 281 | +bool lexical_cast(std::string input, T& output) { | |
| 272 | 282 | logit("Floating lexical cast " + input); |
| 273 | 283 | try{ |
| 274 | 284 | output = (T) std::stold(input); |
| ... | ... | @@ -281,11 +291,9 @@ lexical_cast(std::string input, T& output) { |
| 281 | 291 | } |
| 282 | 292 | |
| 283 | 293 | // String and similar |
| 284 | -template<typename T> | |
| 285 | -typename std::enable_if< | |
| 286 | - !std::is_floating_point<T>::value | |
| 287 | - && !std::is_integral<T>::value, bool>::type | |
| 288 | -lexical_cast(std::string input, T& output) { | |
| 294 | +template<typename T, | |
| 295 | +typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value, detail::enabler>::type = dummy> | |
| 296 | +bool lexical_cast(std::string input, T& output) { | |
| 289 | 297 | logit("Direct lexical cast: " + input); |
| 290 | 298 | output = input; |
| 291 | 299 | return true; |
| ... | ... | @@ -375,9 +383,8 @@ public: |
| 375 | 383 | } |
| 376 | 384 | |
| 377 | 385 | /// Add option for string |
| 378 | - template<typename T> | |
| 379 | - typename std::enable_if<!std::is_array<T>::value, void>::type | |
| 380 | - add_option( | |
| 386 | + template<typename T, DisableIf<std::is_array<T>> = dummy> | |
| 387 | + void add_option( | |
| 381 | 388 | std::string name, ///< The name, long,short |
| 382 | 389 | T &variable, ///< The variable to set |
| 383 | 390 | std::string discription="", ///< Discription string |
| ... | ... | @@ -440,11 +447,10 @@ public: |
| 440 | 447 | } |
| 441 | 448 | |
| 442 | 449 | /// Add option for flag |
| 443 | - template<typename T> | |
| 444 | - typename std::enable_if<std::is_integral<T>::value, void>::type | |
| 445 | - add_flag( | |
| 450 | + template<typename T, EnableIf<std::is_integral<T>> = dummy> | |
| 451 | + void add_flag( | |
| 446 | 452 | std::string name, ///< The name, short,long |
| 447 | - T &count, ///< A varaible holding the count | |
| 453 | + T &count, ///< A varaible holding the count | |
| 448 | 454 | std::string discription="" ///< Discription string |
| 449 | 455 | ) { |
| 450 | 456 | ... | ... |