Commit 5f3e20a906d0aa45ec3acec3b061c559e3908c9a

Authored by Henry Fredrick Schreiner
1 parent 3952addd

C++14 like syntax for enable_if_t

Showing 1 changed file with 21 additions and 15 deletions
include/CLI.hpp
... ... @@ -13,13 +13,18 @@
13 13 #include <iomanip>
14 14 #include <numeric>
15 15  
16   -// This is unreachable outside this file; you should not use Combiner directly
17   -namespace {
  16 +//#define CLI_LOG 1
18 17  
  18 +namespace CLI {
  19 +
  20 +/// Log a message, can be enabled with CLI_LOG
19 21 void logit(std::string) {
20   - //std::cout << "\033[1;31m" << output << "\033[0m" << std::endl;
  22 +#ifdef CLI_LOG
  23 + std::cout << "\033[1;31m" << output << "\033[0m" << std::endl;
  24 +#endif
21 25 }
22 26  
  27 +/// Simple fucntion to join a string
23 28 template <typename T>
24 29 std::string join(const T& v, std::string delim = ",") {
25 30 std::ostringstream s;
... ... @@ -32,22 +37,23 @@ std::string join(const T&amp; v, std::string delim = &quot;,&quot;) {
32 37 return s.str();
33 38 }
34 39  
35   -}
36   -
37   -namespace CLI {
38   -
39 40 // Based generally on https://rmf.io/cxx11/almost-static-if
40 41 namespace detail {
  42 + /// Simple empty scoped class
41 43 enum class enabler {};
42 44 }
  45 +
  46 +/// An instance to use in EnableIf
43 47 constexpr detail::enabler dummy = {};
44 48  
45 49 // Copied from C++14
  50 +#if __cplusplus < 201402L
46 51 template< bool B, class T = void >
47 52 using enable_if_t = typename std::enable_if<B,T>::type;
48   -
49   -template <bool Condition>
50   -using EnableIf = enable_if_t<Condition, detail::enabler>;
  53 +#else
  54 +using std::enable_if_t;
  55 +#endif
  56 +// If your compiler supports C++14, you can use that definition instead
51 57  
52 58 struct Combiner {
53 59 int num;
... ... @@ -265,7 +271,7 @@ public:
265 271 };
266 272  
267 273  
268   -template<typename T, EnableIf<std::is_integral<T>::value> = dummy>
  274 +template<typename T, enable_if_t<std::is_integral<T>::value, detail::enabler> = dummy>
269 275 bool lexical_cast(std::string input, T& output) {
270 276 logit("Int lexical cast " + input);
271 277 try{
... ... @@ -278,7 +284,7 @@ bool lexical_cast(std::string input, T&amp; output) {
278 284 }
279 285 }
280 286  
281   -template<typename T, EnableIf<std::is_floating_point<T>::value> = dummy>
  287 +template<typename T, enable_if_t<std::is_floating_point<T>::value, detail::enabler> = dummy>
282 288 bool lexical_cast(std::string input, T& output) {
283 289 logit("Floating lexical cast " + input);
284 290 try{
... ... @@ -293,7 +299,7 @@ bool lexical_cast(std::string input, T&amp; output) {
293 299  
294 300 // String and similar
295 301 template<typename T,
296   -EnableIf<!std::is_floating_point<T>::value && !std::is_integral<T>::value> = dummy>
  302 +enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value, detail::enabler> = dummy>
297 303 bool lexical_cast(std::string input, T& output) {
298 304 logit("Direct lexical cast: " + input);
299 305 output = input;
... ... @@ -384,7 +390,7 @@ public:
384 390 }
385 391  
386 392 /// Add option for string
387   - template<typename T, EnableIf<!std::is_array<T>::value> = dummy>
  393 + template<typename T, enable_if_t<!std::is_array<T>::value, detail::enabler> = dummy>
388 394 void add_option(
389 395 std::string name, ///< The name, long,short
390 396 T &variable, ///< The variable to set
... ... @@ -448,7 +454,7 @@ public:
448 454 }
449 455  
450 456 /// Add option for flag
451   - template<typename T, EnableIf<std::is_integral<T>::value> = dummy>
  457 + template<typename T, enable_if_t<std::is_integral<T>::value, detail::enabler> = dummy>
452 458 void add_flag(
453 459 std::string name, ///< The name, short,long
454 460 T &count, ///< A varaible holding the count
... ...