CLI11 2.7.2
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
TypeTools.hpp
1// Copyright (c) 2017-2026, University of Cincinnati, developed by Henry Schreiner
2// under NSF AWARD 1414736 and by the respective contributors.
3// All rights reserved.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6
7#pragma once
8
9// IWYU pragma: private, include "CLI/CLI.hpp"
10
11// [CLI11:public_includes:set]
12#include <algorithm>
13#include <cctype>
14#include <cerrno>
15#include <cstddef>
16#include <cstdint>
17#include <cstdlib>
18#include <exception>
19#include <iomanip>
20#include <limits>
21#include <memory>
22#include <sstream>
23#include <string>
24#include <type_traits>
25#include <utility>
26#include <vector>
27// [CLI11:public_includes:end]
28
29#include "Encoding.hpp"
30#include "Macros.hpp"
31#include "StringTools.hpp"
32
33namespace CLI {
34// [CLI11:type_tools_hpp:verbatim]
35
36// Type tools
37
38// Utilities for type enabling
39namespace detail {
40// Based generally on https://rmf.io/cxx11/almost-static-if
42enum class enabler : std::uint8_t {};
43
45CLI11_MODULE_INLINE constexpr enabler dummy = {};
46} // namespace detail
47
53template <bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
54
56template <typename... Ts> struct make_void {
57 using type = void;
58};
59
61template <typename... Ts> using void_t = typename make_void<Ts...>::type;
62
64template <bool B, class T, class F> using conditional_t = typename std::conditional<B, T, F>::type;
65
67template <typename T> struct is_bool : std::false_type {};
68
70template <> struct is_bool<bool> : std::true_type {};
71
73template <typename T> struct is_shared_ptr : std::false_type {};
74
76template <typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
77
79template <typename T> struct is_shared_ptr<const std::shared_ptr<T>> : std::true_type {};
80
82template <typename T> struct is_copyable_ptr {
83 static bool const value = is_shared_ptr<T>::value || std::is_pointer<T>::value;
84};
85
87template <typename T> struct IsMemberType {
88 using type = T;
89};
90
92template <> struct IsMemberType<const char *> {
93 using type = std::string;
94};
95
96namespace adl_detail {
102template <typename T, typename S = std::string> class is_lexical_castable {
103 template <typename TT, typename SS>
104 static auto test(int) -> decltype(lexical_cast(std::declval<const SS &>(), std::declval<TT &>()), std::true_type());
105
106 template <typename, typename> static auto test(...) -> std::false_type;
107
108 public:
109 static constexpr bool value = decltype(test<T, S>(0))::value;
110};
111} // namespace adl_detail
112
113namespace detail {
114
115// These are utilities for IsMember and other transforming objects
116
119
121template <typename T, typename Enable = void> struct element_type {
122 using type = T;
123};
124
125template <typename T> struct element_type<T, typename std::enable_if<is_copyable_ptr<T>::value>::type> {
126 using type = typename std::pointer_traits<T>::element_type;
127};
128
131template <typename T> struct element_value_type {
132 using type = typename element_type<T>::type::value_type;
133};
134
136template <typename T, typename _ = void> struct pair_adaptor : std::false_type {
137 using value_type = typename T::value_type;
138 using first_type = typename std::remove_const<value_type>::type;
139 using second_type = typename std::remove_const<value_type>::type;
140
142 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
143 return std::forward<Q>(pair_value);
144 }
145
146 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::forward<Q>(pair_value)) {
147 return std::forward<Q>(pair_value);
148 }
149};
150
153template <typename T>
155 T,
156 conditional_t<false, void_t<typename T::value_type::first_type, typename T::value_type::second_type>, void>>
157 : std::true_type {
158 using value_type = typename T::value_type;
159 using first_type = typename std::remove_const<typename value_type::first_type>::type;
160 using second_type = typename std::remove_const<typename value_type::second_type>::type;
161
163 template <typename Q> static auto first(Q &&pair_value) -> decltype(std::get<0>(std::forward<Q>(pair_value))) {
164 return std::get<0>(std::forward<Q>(pair_value));
165 }
166
167 template <typename Q> static auto second(Q &&pair_value) -> decltype(std::get<1>(std::forward<Q>(pair_value))) {
168 return std::get<1>(std::forward<Q>(pair_value));
169 }
170};
171
172// Warning is suppressed due to "bug" in gcc<5.0 and gcc 7.0 with c++17 enabled that generates a -Wnarrowing warning
173// in the unevaluated context even if the function that was using this wasn't used. The standard says narrowing in
174// brace initialization shouldn't be allowed but for backwards compatibility gcc allows it in some contexts. It is a
175// little fuzzy what happens in template constructs and I think that was something GCC took a little while to work out.
176// But regardless some versions of gcc generate a warning when they shouldn't from the following code so that should be
177// suppressed
178#ifdef __GNUC__
179#pragma GCC diagnostic push
180#pragma GCC diagnostic ignored "-Wnarrowing"
181#endif
182// check for constructibility from a specific type and copy assignable used in the parse detection
183template <typename T, typename C> class is_direct_constructible {
184 template <typename TT, typename CC>
185 static auto test(int, std::true_type) -> decltype(
186// NVCC warns about narrowing conversions here
187#ifdef __CUDACC__
188#ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
189#pragma nv_diag_suppress 2361
190#else
191#pragma diag_suppress 2361
192#endif
193#endif
194 TT{std::declval<CC>()}
195#ifdef __CUDACC__
196#ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
197#pragma nv_diag_default 2361
198#else
199#pragma diag_default 2361
200#endif
201#endif
202 ,
203 std::is_move_assignable<TT>());
204
205 template <typename TT, typename CC> static auto test(int, std::false_type) -> std::false_type;
206
207 template <typename, typename> static auto test(...) -> std::false_type;
208
209 public:
210 static constexpr bool value = decltype(test<T, C>(0, typename std::is_constructible<T, C>::type()))::value;
211};
212#ifdef __GNUC__
213#pragma GCC diagnostic pop
214#endif
215
216// Check for output streamability
217// Based on https://stackoverflow.com/questions/22758291/how-can-i-detect-if-a-type-can-be-streamed-to-an-stdostream
218
219template <typename T, typename S = std::ostringstream> class is_ostreamable {
220 template <typename TT, typename SS>
221 static auto test(int) -> decltype(std::declval<SS &>() << std::declval<TT>(), std::true_type());
222
223 template <typename, typename> static auto test(...) -> std::false_type;
224
225 public:
226 static constexpr bool value = decltype(test<T, S>(0))::value;
227};
228
230template <typename T, typename S = std::istringstream> class is_istreamable {
231 template <typename TT, typename SS>
232 static auto test(int) -> decltype(std::declval<SS &>() >> std::declval<TT &>(), std::true_type());
233
234 template <typename, typename> static auto test(...) -> std::false_type;
235
236 public:
237 static constexpr bool value = decltype(test<T, S>(0))::value;
238};
239
241template <typename T> class is_complex {
242 template <typename TT>
243 static auto test(int) -> decltype(std::declval<TT>().real(), std::declval<TT>().imag(), std::true_type());
244
245 template <typename> static auto test(...) -> std::false_type;
246
247 public:
248 static constexpr bool value = decltype(test<T>(0))::value;
249};
250
252template <typename T, enable_if_t<is_istreamable<T>::value, detail::enabler> = detail::dummy>
253bool from_stream(const std::string &istring, T &obj) {
254 std::istringstream is;
255 is.str(istring);
256 is >> obj;
257 return !is.fail() && !is.rdbuf()->in_avail();
258}
259
260template <typename T, enable_if_t<!is_istreamable<T>::value, detail::enabler> = detail::dummy>
261bool from_stream(const std::string & /*istring*/, T & /*obj*/) {
262 return false;
263}
264
265// check to see if an object is a mutable container (fail by default)
266template <typename T, typename _ = void> struct is_mutable_container : std::false_type {};
267
271template <typename T>
273 T,
274 conditional_t<false,
275 void_t<typename T::value_type,
276 decltype(std::declval<T>().end()),
277 decltype(std::declval<T>().clear()),
278 decltype(std::declval<T>().insert(std::declval<decltype(std::declval<T>().end())>(),
279 std::declval<const typename T::value_type &>()))>,
280 void>> : public conditional_t<std::is_constructible<T, std::string>::value ||
281 std::is_constructible<T, std::wstring>::value,
282 std::false_type,
283 std::true_type> {};
284
285// check to see if an object is a mutable container (fail by default)
286template <typename T, typename _ = void> struct is_readable_container : std::false_type {};
287
290template <typename T>
292 T,
293 conditional_t<false, void_t<decltype(std::declval<T>().end()), decltype(std::declval<T>().begin())>, void>>
294 : public std::true_type {};
295
296// check to see if an object is a wrapper (fail by default)
297template <typename T, typename _ = void> struct is_wrapper : std::false_type {};
298
299// check if an object is a wrapper (it has a value_type defined)
300template <typename T>
301struct is_wrapper<T, conditional_t<false, void_t<typename T::value_type>, void>> : public std::true_type {};
302
303// Check for tuple like types, as in classes with a tuple_size type trait
304// Even though in C++26 std::complex gains a std::tuple interface, for our purposes we treat is as NOT a tuple
305template <typename S> class is_tuple_like {
306 template <typename SS, enable_if_t<!is_complex<SS>::value, detail::enabler> = detail::dummy>
307 // static auto test(int)
308 // -> decltype(std::conditional<(std::tuple_size<SS>::value > 0), std::true_type, std::false_type>::type());
309 static auto test(int) -> decltype(std::tuple_size<typename std::decay<SS>::type>::value, std::true_type{});
310 template <typename> static auto test(...) -> std::false_type;
311
312 public:
313 static constexpr bool value = decltype(test<S>(0))::value;
314};
315
317template <typename T, typename Enable = void> struct type_count_base {
318 static const int value{0};
319};
320
322template <typename T>
324 typename std::enable_if<!is_tuple_like<T>::value && !is_mutable_container<T>::value &&
325 !std::is_void<T>::value>::type> {
326 static constexpr int value{1};
327};
328
330template <typename T>
331struct type_count_base<T, typename std::enable_if<is_tuple_like<T>::value && !is_mutable_container<T>::value>::type> {
332 static constexpr int value{// cppcheck-suppress unusedStructMember
333 std::tuple_size<typename std::decay<T>::type>::value};
334};
335
337template <typename T> struct type_count_base<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
338 static constexpr int value{type_count_base<typename T::value_type>::value};
339};
340
342template <typename T, enable_if_t<std::is_convertible<T, std::string>::value, detail::enabler> = detail::dummy>
343auto to_string(T &&value) -> decltype(std::forward<T>(value)) {
344 return std::forward<T>(value);
345}
346
348template <typename T,
349 enable_if_t<std::is_constructible<std::string, T>::value && !std::is_convertible<T, std::string>::value,
350 detail::enabler> = detail::dummy>
351std::string to_string(T &&value) {
352 return std::string(value); // NOLINT(google-readability-casting)
353}
354
355template <typename T,
356 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
357 is_ostreamable<T>::value && std::is_floating_point<typename std::decay<T>::type>::value,
358 detail::enabler> = detail::dummy>
359std::string to_string(T &&value) {
360 std::ostringstream stream;
361 stream << std::setprecision(std::numeric_limits<typename std::decay<T>::type>::max_digits10) << value;
362 return stream.str();
363}
364
366template <typename T,
367 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
368 is_ostreamable<T>::value && !std::is_floating_point<typename std::decay<T>::type>::value,
369 detail::enabler> = detail::dummy>
370std::string to_string(T &&value) {
371 std::stringstream stream;
372 stream << value;
373 return stream.str();
374}
375
376// additional forward declarations
377
379template <typename T,
380 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
381 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value == 1,
382 detail::enabler> = detail::dummy>
383inline std::string to_string(T &&value);
384
386template <typename T,
387 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
388 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value >= 2,
389 detail::enabler> = detail::dummy>
390inline std::string to_string(T &&value);
391
393template <
394 typename T,
395 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
396 !is_ostreamable<T>::value && !is_readable_container<typename std::remove_const<T>::type>::value &&
397 !is_tuple_like<T>::value,
398 detail::enabler> = detail::dummy>
399inline std::string to_string(T &&) {
400 return {};
401}
402
404template <typename T,
405 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
406 !is_ostreamable<T>::value && is_readable_container<T>::value && !is_tuple_like<T>::value,
407 detail::enabler> = detail::dummy>
408inline std::string to_string(T &&variable) {
409 auto cval = variable.begin();
410 auto end = variable.end();
411 if(cval == end) {
412 return {"{}"};
413 }
414 std::vector<std::string> defaults;
415 while(cval != end) {
416 defaults.emplace_back(CLI::detail::to_string(*cval));
417 ++cval;
418 }
419 return {"[" + detail::join(defaults) + "]"};
420}
421
423
425template <typename T, std::size_t I>
426inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_value_string(T && /*value*/);
427
429template <typename T, std::size_t I>
430inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_value_string(T &&value);
431
433template <typename T,
434 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
435 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value == 1,
436 detail::enabler>>
437inline std::string to_string(T &&value) {
438 return to_string(std::get<0>(value));
439}
440
442template <typename T,
443 enable_if_t<!std::is_convertible<T, std::string>::value && !std::is_constructible<std::string, T>::value &&
444 !is_ostreamable<T>::value && is_tuple_like<T>::value && type_count_base<T>::value >= 2,
445 detail::enabler>>
446inline std::string to_string(T &&value) {
447 auto tname = std::string(1, '[') + tuple_value_string<T, 0>(value);
448 tname.push_back(']');
449 return tname;
450}
451
453template <typename T, std::size_t I>
454inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_value_string(T && /*value*/) {
455 return std::string{};
456}
457
459template <typename T, std::size_t I>
460inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_value_string(T &&value) {
461 auto str = std::string{to_string(std::get<I>(value))} + ',' + tuple_value_string<T, I + 1>(value);
462 if(str.back() == ',')
463 str.pop_back();
464 return str;
465}
466
468template <typename T1,
469 typename T2,
470 typename T,
471 enable_if_t<std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
472auto checked_to_string(T &&value) -> decltype(to_string(std::forward<T>(value))) {
473 return to_string(std::forward<T>(value));
474}
475
477template <typename T1,
478 typename T2,
479 typename T,
480 enable_if_t<!std::is_same<T1, T2>::value, detail::enabler> = detail::dummy>
481std::string checked_to_string(T &&) {
482 return std::string{};
483}
485template <typename T, enable_if_t<std::is_integral<T>::value, detail::enabler> = detail::dummy>
486std::string value_string(const T &value) {
487 return std::to_string(value);
488}
489
491template <typename T, enable_if_t<std::is_floating_point<T>::value, detail::enabler> = detail::dummy>
492std::string value_string(const T &value) {
493 std::ostringstream stream;
494 stream << std::setprecision(std::numeric_limits<T>::max_digits10) << value;
495 return stream.str();
496}
497
499template <typename T, enable_if_t<std::is_enum<T>::value, detail::enabler> = detail::dummy>
500std::string value_string(const T &value) {
501 return std::to_string(static_cast<typename std::underlying_type<T>::type>(value));
502}
504template <typename T,
505 enable_if_t<!std::is_enum<T>::value && !std::is_arithmetic<T>::value, detail::enabler> = detail::dummy>
506auto value_string(const T &value) -> decltype(to_string(value)) {
507 return to_string(value);
508}
509
511template <typename T, typename def, typename Enable = void> struct wrapped_type {
512 using type = def;
513};
514
516template <typename T, typename def> struct wrapped_type<T, def, typename std::enable_if<is_wrapper<T>::value>::type> {
517 using type = typename T::value_type;
518};
519
521
523template <typename T> struct subtype_count;
524
526template <typename T> struct subtype_count_min;
527
529template <typename T, typename Enable = void> struct type_count {
530 static const int value{0};
531};
532
534template <typename T>
535struct type_count<T,
536 typename std::enable_if<!is_wrapper<T>::value && !is_tuple_like<T>::value && !is_complex<T>::value &&
537 !std::is_void<T>::value>::type> {
538 static constexpr int value{1};
539};
540
542template <typename T> struct type_count<T, typename std::enable_if<is_complex<T>::value>::type> {
543 static constexpr int value{2};
544};
545
547template <typename T> struct type_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
548 static constexpr int value{subtype_count<typename T::value_type>::value};
549};
550
552template <typename T>
553struct type_count<T,
554 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value &&
555 !is_mutable_container<T>::value>::type> {
556 static constexpr int value{type_count<typename T::value_type>::value};
557};
558
560template <typename T, std::size_t I>
561constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size() {
562 return 0;
563}
564
566template <typename T, std::size_t I>
567 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size() {
568 return subtype_count<typename std::tuple_element<I, T>::type>::value + tuple_type_size<T, I + 1>();
569}
570
572template <typename T>
573struct type_count<T, typename std::enable_if<is_tuple_like<T>::value && !is_complex<T>::value>::type> {
574 static constexpr int value{tuple_type_size<T, 0>()};
575};
576
578template <typename T> struct subtype_count {
579 static constexpr int value{is_mutable_container<T>::value ? expected_max_vector_size : type_count<T>::value};
580};
581
583template <typename T, typename Enable = void> struct type_count_min {
584 static const int value{0};
585};
586
588template <typename T>
589struct type_count_min<
590 T,
591 typename std::enable_if<!is_mutable_container<T>::value && !is_tuple_like<T>::value && !is_wrapper<T>::value &&
592 !is_complex<T>::value && !std::is_void<T>::value>::type> {
593 static constexpr int value{type_count<T>::value};
594};
595
597template <typename T> struct type_count_min<T, typename std::enable_if<is_complex<T>::value>::type> {
598 static constexpr int value{1};
599};
600
602template <typename T>
603struct type_count_min<
604 T,
605 typename std::enable_if<is_wrapper<T>::value && !is_complex<T>::value && !is_tuple_like<T>::value>::type> {
606 static constexpr int value{subtype_count_min<typename T::value_type>::value};
607};
608
610template <typename T, std::size_t I>
611constexpr typename std::enable_if<I == type_count_base<T>::value, int>::type tuple_type_size_min() {
612 return 0;
613}
614
616template <typename T, std::size_t I>
617 constexpr typename std::enable_if < I<type_count_base<T>::value, int>::type tuple_type_size_min() {
618 return subtype_count_min<typename std::tuple_element<I, T>::type>::value + tuple_type_size_min<T, I + 1>();
619}
620
622template <typename T>
623struct type_count_min<T, typename std::enable_if<is_tuple_like<T>::value && !is_complex<T>::value>::type> {
624 static constexpr int value{tuple_type_size_min<T, 0>()};
625};
626
628template <typename T> struct subtype_count_min {
629 static constexpr int value{is_mutable_container<T>::value
630 ? ((type_count<T>::value < expected_max_vector_size) ? type_count<T>::value : 0)
631 : type_count_min<T>::value};
632};
633
635template <typename T, typename Enable = void> struct expected_count {
636 static const int value{0};
637};
638
640template <typename T>
641struct expected_count<T,
642 typename std::enable_if<!is_mutable_container<T>::value && !is_wrapper<T>::value &&
643 !std::is_void<T>::value>::type> {
644 static constexpr int value{1};
645};
647template <typename T> struct expected_count<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
648 static constexpr int value{expected_max_vector_size};
649};
650
652template <typename T>
653struct expected_count<T, typename std::enable_if<!is_mutable_container<T>::value && is_wrapper<T>::value>::type> {
654 static constexpr int value{expected_count<typename T::value_type>::value};
655};
656
657// Enumeration of the different supported categorizations of objects
658enum class object_category : std::uint8_t {
659 char_value = 1,
660 integral_value = 2,
661 unsigned_integral = 4,
662 enumeration = 6,
663 boolean_value = 8,
664 floating_point = 10,
665 number_constructible = 12,
666 double_constructible = 14,
667 integer_constructible = 16,
668 // string like types
669 string_assignable = 23,
670 string_constructible = 24,
671 wstring_assignable = 25,
672 wstring_constructible = 26,
673 other = 45,
674 // special wrapper or container types
675 wrapper_value = 50,
676 complex_number = 60,
677 tuple_value = 70,
678 container_value = 80,
679
680};
681
683
685template <typename T, typename Enable = void> struct classify_object {
686 static constexpr object_category value{object_category::other};
687};
688
690template <typename T>
691struct classify_object<
692 T,
693 typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, char>::value && std::is_signed<T>::value &&
694 !is_bool<T>::value && !std::is_enum<T>::value>::type> {
695 static constexpr object_category value{object_category::integral_value};
696};
697
699template <typename T>
700struct classify_object<T,
701 typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value &&
702 !std::is_same<T, char>::value && !is_bool<T>::value>::type> {
703 static constexpr object_category value{object_category::unsigned_integral};
704};
705
707template <typename T>
708struct classify_object<T, typename std::enable_if<std::is_same<T, char>::value && !std::is_enum<T>::value>::type> {
709 static constexpr object_category value{object_category::char_value};
710};
711
713template <typename T> struct classify_object<T, typename std::enable_if<is_bool<T>::value>::type> {
714 static constexpr object_category value{object_category::boolean_value};
715};
716
718template <typename T> struct classify_object<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
719 static constexpr object_category value{object_category::floating_point};
720};
721#if defined _MSC_VER
722// in MSVC wstring should take precedence if available this isn't as useful on other compilers due to the broader use of
723// utf-8 encoding
724#define WIDE_STRING_CHECK \
725 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value
726#define STRING_CHECK true
727#else
728#define WIDE_STRING_CHECK true
729#define STRING_CHECK !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value
730#endif
731
733template <typename T>
734struct classify_object<
735 T,
736 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value && WIDE_STRING_CHECK &&
737 std::is_assignable<T &, std::string>::value>::type> {
738 static constexpr object_category value{object_category::string_assignable};
739};
740
742template <typename T>
743struct classify_object<
744 T,
745 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
746 !std::is_assignable<T &, std::string>::value && (type_count<T>::value == 1) &&
747 WIDE_STRING_CHECK && std::is_constructible<T, std::string>::value>::type> {
748 static constexpr object_category value{object_category::string_constructible};
749};
750
752template <typename T>
753struct classify_object<T,
754 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
755 STRING_CHECK && std::is_assignable<T &, std::wstring>::value>::type> {
756 static constexpr object_category value{object_category::wstring_assignable};
757};
758
759template <typename T>
760struct classify_object<
761 T,
762 typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value &&
763 !std::is_assignable<T &, std::wstring>::value && (type_count<T>::value == 1) &&
764 STRING_CHECK && std::is_constructible<T, std::wstring>::value>::type> {
765 static constexpr object_category value{object_category::wstring_constructible};
766};
767
769template <typename T> struct classify_object<T, typename std::enable_if<std::is_enum<T>::value>::type> {
770 static constexpr object_category value{object_category::enumeration};
771};
772
773template <typename T> struct classify_object<T, typename std::enable_if<is_complex<T>::value>::type> {
774 static constexpr object_category value{object_category::complex_number};
775};
776
779template <typename T> struct uncommon_type {
780 using type = typename std::conditional<
781 !std::is_floating_point<T>::value && !std::is_integral<T>::value &&
782 !std::is_assignable<T &, std::string>::value && !std::is_constructible<T, std::string>::value &&
783 !std::is_assignable<T &, std::wstring>::value && !std::is_constructible<T, std::wstring>::value &&
784 !is_complex<T>::value && !is_mutable_container<T>::value && !std::is_enum<T>::value,
785 std::true_type,
786 std::false_type>::type;
787 static constexpr bool value = type::value;
788};
789
791template <typename T>
792struct classify_object<T,
793 typename std::enable_if<(!is_mutable_container<T>::value && is_wrapper<T>::value &&
794 !is_tuple_like<T>::value && uncommon_type<T>::value)>::type> {
795 static constexpr object_category value{object_category::wrapper_value};
796};
797
799template <typename T>
800struct classify_object<T,
801 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
802 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
803 is_direct_constructible<T, int>::value>::type> {
804 static constexpr object_category value{object_category::number_constructible};
805};
806
808template <typename T>
809struct classify_object<T,
810 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
811 !is_wrapper<T>::value && !is_direct_constructible<T, double>::value &&
812 is_direct_constructible<T, int>::value>::type> {
813 static constexpr object_category value{object_category::integer_constructible};
814};
815
817template <typename T>
818struct classify_object<T,
819 typename std::enable_if<uncommon_type<T>::value && type_count<T>::value == 1 &&
820 !is_wrapper<T>::value && is_direct_constructible<T, double>::value &&
821 !is_direct_constructible<T, int>::value>::type> {
822 static constexpr object_category value{object_category::double_constructible};
823};
824
826template <typename T>
827struct classify_object<
828 T,
829 typename std::enable_if<is_tuple_like<T>::value &&
830 ((type_count<T>::value >= 2 && !is_wrapper<T>::value) ||
831 (uncommon_type<T>::value && !is_direct_constructible<T, double>::value &&
832 !is_direct_constructible<T, int>::value) ||
833 (uncommon_type<T>::value && type_count<T>::value >= 2))>::type> {
834 static constexpr object_category value{object_category::tuple_value};
835 // the condition on this class requires it be like a tuple, but on some compilers (like Xcode) tuples can be
836 // constructed from just the first element so tuples of <string, int,int> can be constructed from a string, which
837 // could lead to issues so there are two variants of the condition, the first isolates things with a type size >=2
838 // mainly to get tuples on Xcode with the exception of wrappers, the second is the main one and just separating out
839 // those cases that are caught by other object classifications
840};
841
843template <typename T> struct classify_object<T, typename std::enable_if<is_mutable_container<T>::value>::type> {
844 static constexpr object_category value{object_category::container_value};
845};
846
847// Type name print
848
852
853template <typename T,
854 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
855constexpr const char *type_name() {
856 return "CHAR";
857}
858
859template <typename T,
860 enable_if_t<classify_object<T>::value == object_category::integral_value ||
861 classify_object<T>::value == object_category::integer_constructible,
862 detail::enabler> = detail::dummy>
863constexpr const char *type_name() {
864 return "INT";
865}
866
867template <typename T,
868 enable_if_t<classify_object<T>::value == object_category::unsigned_integral, detail::enabler> = detail::dummy>
869constexpr const char *type_name() {
870 return "UINT";
871}
872
873template <typename T,
874 enable_if_t<classify_object<T>::value == object_category::floating_point ||
875 classify_object<T>::value == object_category::number_constructible ||
876 classify_object<T>::value == object_category::double_constructible,
877 detail::enabler> = detail::dummy>
878constexpr const char *type_name() {
879 return "FLOAT";
880}
881
883template <typename T,
884 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
885constexpr const char *type_name() {
886 return "ENUM";
887}
888
890template <typename T,
891 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
892constexpr const char *type_name() {
893 return "BOOLEAN";
894}
895
897template <typename T,
898 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
899constexpr const char *type_name() {
900 return "COMPLEX";
901}
902
904template <typename T,
905 enable_if_t<classify_object<T>::value >= object_category::string_assignable &&
906 classify_object<T>::value <= object_category::other,
907 detail::enabler> = detail::dummy>
908constexpr const char *type_name() {
909 return "TEXT";
910}
912template <typename T,
913 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
914 detail::enabler> = detail::dummy>
915std::string type_name(); // forward declaration
916
918template <typename T,
919 enable_if_t<classify_object<T>::value == object_category::container_value ||
920 classify_object<T>::value == object_category::wrapper_value,
921 detail::enabler> = detail::dummy>
922std::string type_name(); // forward declaration
923
925template <typename T,
926 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value == 1,
927 detail::enabler> = detail::dummy>
928inline std::string type_name() {
929 return type_name<typename std::decay<typename std::tuple_element<0, T>::type>::type>();
930}
931
933template <typename T, std::size_t I>
934inline typename std::enable_if<I == type_count_base<T>::value, std::string>::type tuple_name() {
935 return std::string{};
936}
937
939template <typename T, std::size_t I>
940inline typename std::enable_if<(I < type_count_base<T>::value), std::string>::type tuple_name() {
941 auto str = std::string{type_name<typename std::decay<typename std::tuple_element<I, T>::type>::type>()} + ',' +
942 tuple_name<T, I + 1>();
943 if(str.back() == ',')
944 str.pop_back();
945 return str;
946}
947
949template <typename T,
950 enable_if_t<classify_object<T>::value == object_category::tuple_value && type_count_base<T>::value >= 2,
951 detail::enabler>>
952inline std::string type_name() {
953 auto tname = std::string(1, '[') + tuple_name<T, 0>();
954 tname.push_back(']');
955 return tname;
956}
957
959template <typename T,
960 enable_if_t<classify_object<T>::value == object_category::container_value ||
961 classify_object<T>::value == object_category::wrapper_value,
962 detail::enabler>>
963inline std::string type_name() {
964 return type_name<typename T::value_type>();
965}
966
967// Lexical cast
968
970template <typename T, enable_if_t<std::is_unsigned<T>::value, detail::enabler> = detail::dummy>
971bool integral_conversion(const std::string &input, T &output) noexcept {
972 if(input.empty()) {
973 return false;
974 }
975 // strtoull skips leading whitespace and silently wraps a negative value, so reject any input whose
976 // first non-whitespace character is a minus sign before it reaches strtoull
977 auto first_non_ws = input.find_first_not_of(" \t\n\v\f\r");
978 if(first_non_ws != std::string::npos && input[first_non_ws] == '-') {
979 return false;
980 }
981 char *val{nullptr};
982 errno = 0;
983 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
984 if(errno == ERANGE) {
985 return false;
986 }
987 output = static_cast<T>(output_ll);
988 if(val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll) {
989 return true;
990 }
991 val = nullptr;
992 std::int64_t output_sll = std::strtoll(input.c_str(), &val, 0);
993 if(val == (input.c_str() + input.size())) {
994 output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
995 return (static_cast<std::int64_t>(output) == output_sll);
996 }
997 // remove separators if present
998 auto group_separators = get_group_separators();
999 if(input.find_first_of(group_separators) != std::string::npos) {
1000 std::string nstring = input;
1001 for(auto &separator : group_separators) {
1002 if(input.find_first_of(separator) != std::string::npos) {
1003 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
1004 }
1005 }
1006 return integral_conversion(nstring, output);
1007 }
1008
1009 if(std::isspace(static_cast<unsigned char>(input.back()))) {
1010 return integral_conversion(trim_copy(input), output);
1011 }
1012 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
1013 val = nullptr;
1014 errno = 0;
1015 output_ll = std::strtoull(input.c_str() + 2, &val, 8);
1016 if(errno == ERANGE) {
1017 return false;
1018 }
1019 output = static_cast<T>(output_ll);
1020 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
1021 }
1022 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
1023 // LCOV_EXCL_START
1024 // In some new compilers including the coverage testing one binary strings are handled properly in strtoull
1025 // automatically so this coverage is missing but is well tested in other compilers
1026 val = nullptr;
1027 errno = 0;
1028 output_ll = std::strtoull(input.c_str() + 2, &val, 2);
1029 if(errno == ERANGE) {
1030 return false;
1031 }
1032 output = static_cast<T>(output_ll);
1033 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
1034 // LCOV_EXCL_STOP
1035 }
1036 return false;
1037}
1038
1040template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
1041bool integral_conversion(const std::string &input, T &output) noexcept {
1042 if(input.empty()) {
1043 return false;
1044 }
1045 char *val = nullptr;
1046 errno = 0;
1047 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
1048 if(errno == ERANGE) {
1049 return false;
1050 }
1051 output = static_cast<T>(output_ll);
1052 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
1053 return true;
1054 }
1055 if(input == "true") {
1056 // this is to deal with a few oddities with flags and wrapper int types
1057 output = static_cast<T>(1);
1058 return true;
1059 }
1060 // remove separators if present
1061 auto group_separators = get_group_separators();
1062 if(input.find_first_of(group_separators) != std::string::npos) {
1063 for(auto &separator : group_separators) {
1064 if(input.find_first_of(separator) != std::string::npos) {
1065 std::string nstring = input;
1066 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
1067 return integral_conversion(nstring, output);
1068 }
1069 }
1070 }
1071 if(std::isspace(static_cast<unsigned char>(input.back()))) {
1072 return integral_conversion(trim_copy(input), output);
1073 }
1074 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
1075 val = nullptr;
1076 errno = 0;
1077 output_ll = std::strtoll(input.c_str() + 2, &val, 8);
1078 if(errno == ERANGE) {
1079 return false;
1080 }
1081 output = static_cast<T>(output_ll);
1082 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1083 }
1084 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
1085 // LCOV_EXCL_START
1086 // In some new compilers including the coverage testing one binary strings are handled properly in strtoll
1087 // automatically so this coverage is missing but is well tested in other compilers
1088 val = nullptr;
1089 errno = 0;
1090 output_ll = std::strtoll(input.c_str() + 2, &val, 2);
1091 if(errno == ERANGE) {
1092 return false;
1093 }
1094 output = static_cast<T>(output_ll);
1095 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1096 // LCOV_EXCL_STOP
1097 }
1098 return false;
1099}
1100
1102CLI11_INLINE std::int64_t to_flag_value(std::string val) noexcept;
1103
1105template <typename T,
1106 enable_if_t<classify_object<T>::value == object_category::integral_value ||
1107 classify_object<T>::value == object_category::unsigned_integral,
1108 detail::enabler> = detail::dummy>
1109bool lexical_cast(const std::string &input, T &output) {
1110 return integral_conversion(input, output);
1111}
1112
1114template <typename T,
1115 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
1116bool lexical_cast(const std::string &input, T &output) {
1117 if(input.size() == 1) {
1118 output = static_cast<T>(input[0]);
1119 return true;
1120 }
1121 std::int8_t res{0};
1122 // we do it this way as some systems have char as signed and not, this ensures consistency in the way things are
1123 // handled
1124 bool result = integral_conversion(input, res);
1125 if(result) {
1126 output = static_cast<T>(res);
1127 }
1128 return result;
1129}
1130
1132template <typename T,
1133 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
1134bool lexical_cast(const std::string &input, T &output) {
1135 errno = 0;
1136 auto out = to_flag_value(input);
1137 if(errno == 0) {
1138 output = (out > 0);
1139 } else if(errno == ERANGE) {
1140 output = (input[0] != '-');
1141 } else {
1142 return false;
1143 }
1144 return true;
1145}
1146
1148template <typename T,
1149 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
1150bool lexical_cast(const std::string &input, T &output) {
1151 if(input.empty()) {
1152 return false;
1153 }
1154 char *val = nullptr;
1155 auto output_ld = std::strtold(input.c_str(), &val);
1156 // strtold performs no conversion (and leaves val == start) for inputs like whitespace-only strings;
1157 // treat that as a failure rather than reporting a successful conversion to 0
1158 if(val == input.c_str()) {
1159 return false;
1160 }
1161 output = static_cast<T>(output_ld);
1162 if(val == (input.c_str() + input.size())) {
1163 return true;
1164 }
1165 while(std::isspace(static_cast<unsigned char>(*val))) {
1166 ++val;
1167 if(val == (input.c_str() + input.size())) {
1168 return true;
1169 }
1170 }
1171
1172 // remove separators if present
1173 auto group_separators = get_group_separators();
1174 if(input.find_first_of(group_separators) != std::string::npos) {
1175 for(auto &separator : group_separators) {
1176 if(input.find_first_of(separator) != std::string::npos) {
1177 std::string nstring = input;
1178 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
1179 return lexical_cast(nstring, output);
1180 }
1181 }
1182 }
1183 return false;
1184}
1185
1187template <typename T,
1188 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
1189bool lexical_cast(const std::string &input, T &output) {
1190 using XC = typename wrapped_type<T, double>::type;
1191 XC x{0.0}, y{0.0};
1192 auto str1 = input;
1193 bool worked = false;
1194 auto nloc = str1.find_last_of("+-");
1195 if(nloc != std::string::npos && nloc > 0) {
1196 worked = lexical_cast(str1.substr(0, nloc), x);
1197 str1 = str1.substr(nloc);
1198 if(str1.back() == 'i' || str1.back() == 'j')
1199 str1.pop_back();
1200 worked = worked && lexical_cast(str1, y);
1201 } else {
1202 if(str1.back() == 'i' || str1.back() == 'j') {
1203 str1.pop_back();
1204 worked = lexical_cast(str1, y);
1205 x = XC{0};
1206 } else {
1207 worked = lexical_cast(str1, x);
1208 y = XC{0};
1209 }
1210 }
1211 if(worked) {
1212 output = T{x, y};
1213 return worked;
1214 }
1215 return from_stream(input, output);
1216}
1217
1219template <typename T,
1220 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
1221bool lexical_cast(const std::string &input, T &output) {
1222 output = input;
1223 return true;
1224}
1225
1227template <
1228 typename T,
1229 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
1230bool lexical_cast(const std::string &input, T &output) {
1231 output = T(input);
1232 return true;
1233}
1234
1236template <
1237 typename T,
1238 enable_if_t<classify_object<T>::value == object_category::wstring_assignable, detail::enabler> = detail::dummy>
1239bool lexical_cast(const std::string &input, T &output) {
1240 output = widen(input);
1241 return true;
1242}
1243
1244template <
1245 typename T,
1246 enable_if_t<classify_object<T>::value == object_category::wstring_constructible, detail::enabler> = detail::dummy>
1247bool lexical_cast(const std::string &input, T &output) {
1248 output = T{widen(input)};
1249 return true;
1250}
1251
1253template <typename T,
1254 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
1255bool lexical_cast(const std::string &input, T &output) {
1256 typename std::underlying_type<T>::type val;
1257 if(!integral_conversion(input, val)) {
1258 return false;
1259 }
1260 output = static_cast<T>(val);
1261 return true;
1262}
1263
1265template <typename T,
1266 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1267 std::is_assignable<T &, typename T::value_type>::value,
1268 detail::enabler> = detail::dummy>
1269bool lexical_cast(const std::string &input, T &output) {
1270 typename T::value_type val;
1271 if(lexical_cast(input, val)) {
1272 output = val;
1273 return true;
1274 }
1275 return from_stream(input, output);
1276}
1277
1278template <typename T,
1279 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1280 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
1281 detail::enabler> = detail::dummy>
1282bool lexical_cast(const std::string &input, T &output) {
1283 typename T::value_type val;
1284 if(lexical_cast(input, val)) {
1285 output = T{val};
1286 return true;
1287 }
1288 return from_stream(input, output);
1289}
1290
1292template <
1293 typename T,
1294 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1295bool lexical_cast(const std::string &input, T &output) {
1296 int val = 0;
1297 if(integral_conversion(input, val)) {
1298 output = T(val);
1299 return true;
1300 }
1301
1302 double dval = 0.0;
1303 if(lexical_cast(input, dval)) {
1304 output = T{dval};
1305 return true;
1306 }
1307
1308 return from_stream(input, output);
1309}
1310
1312template <
1313 typename T,
1314 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1315bool lexical_cast(const std::string &input, T &output) {
1316 int val = 0;
1317 if(integral_conversion(input, val)) {
1318 output = T(val);
1319 return true;
1320 }
1321 return from_stream(input, output);
1322}
1323
1325template <
1326 typename T,
1327 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1328bool lexical_cast(const std::string &input, T &output) {
1329 double val = 0.0;
1330 if(lexical_cast(input, val)) {
1331 output = T{val};
1332 return true;
1333 }
1334 return from_stream(input, output);
1335}
1336
1338template <typename T,
1339 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1340 detail::enabler> = detail::dummy>
1341bool lexical_cast(const std::string &input, T &output) {
1342 int val = 0;
1343 if(integral_conversion(input, val)) {
1344#ifdef _MSC_VER
1345#pragma warning(push)
1346#pragma warning(disable : 4800)
1347#endif
1348 // with Atomic<XX> this could produce a warning due to the conversion but if atomic gets here it is an old style
1349 // so will most likely still work
1350 output = val;
1351#ifdef _MSC_VER
1352#pragma warning(pop)
1353#endif
1354 return true;
1355 }
1356 // LCOV_EXCL_START
1357 // This version of cast is only used for odd cases in an older compilers the fail over
1358 // from_stream is tested elsewhere an not relevant for coverage here
1359 return from_stream(input, output);
1360 // LCOV_EXCL_STOP
1361}
1362
1364template <typename T,
1365 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1366 is_istreamable<T>::value,
1367 detail::enabler> = detail::dummy>
1368bool lexical_cast(const std::string &input, T &output) {
1369 return from_stream(input, output);
1370}
1371
1374template <typename T,
1375 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1376 !is_istreamable<T>::value && !adl_detail::is_lexical_castable<T>::value,
1377 detail::enabler> = detail::dummy>
1378bool lexical_cast(const std::string & /*input*/, T & /*output*/) {
1379 static_assert(!std::is_same<T, T>::value, // Can't just write false here.
1380 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1381 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1382 return false;
1383}
1384
1387template <typename AssignTo,
1388 typename ConvertTo,
1389 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !is_wrapper<AssignTo>::value &&
1390 (classify_object<AssignTo>::value == object_category::string_assignable ||
1391 classify_object<AssignTo>::value == object_category::string_constructible ||
1392 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1393 classify_object<AssignTo>::value == object_category::wstring_constructible),
1394 detail::enabler> = detail::dummy>
1395bool lexical_assign(const std::string &input, AssignTo &output) {
1396 return lexical_cast(input, output);
1397}
1398
1401template <typename AssignTo,
1402 typename ConvertTo,
1403 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && is_wrapper<AssignTo>::value &&
1404 (classify_object<AssignTo>::value == object_category::string_assignable ||
1405 classify_object<AssignTo>::value == object_category::string_constructible ||
1406 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1407 classify_object<AssignTo>::value == object_category::wstring_constructible),
1408 detail::enabler> = detail::dummy>
1409bool lexical_assign(const std::string &input, AssignTo &output) {
1410 if(input.empty()) {
1411 output = AssignTo{};
1412 return true;
1413 }
1414 return lexical_cast(input, output);
1415}
1416
1418template <typename AssignTo,
1419 typename ConvertTo,
1420 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::value &&
1421 classify_object<AssignTo>::value != object_category::string_assignable &&
1422 classify_object<AssignTo>::value != object_category::string_constructible &&
1423 classify_object<AssignTo>::value != object_category::wstring_assignable &&
1424 classify_object<AssignTo>::value != object_category::wstring_constructible,
1425 detail::enabler> = detail::dummy>
1426bool lexical_assign(const std::string &input, AssignTo &output) {
1427 if(input.empty()) {
1428 output = AssignTo{};
1429 return true;
1430 }
1431
1432 return lexical_cast(input, output);
1433} // LCOV_EXCL_LINE
1434
1436template <typename AssignTo,
1437 typename ConvertTo,
1438 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1439 classify_object<AssignTo>::value == object_category::wrapper_value,
1440 detail::enabler> = detail::dummy>
1441bool lexical_assign(const std::string &input, AssignTo &output) {
1442 if(input.empty()) {
1443 typename AssignTo::value_type emptyVal{};
1444 output = emptyVal;
1445 return true;
1446 }
1447 return lexical_cast(input, output);
1448}
1449
1452template <typename AssignTo,
1453 typename ConvertTo,
1454 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1455 classify_object<AssignTo>::value != object_category::wrapper_value &&
1456 std::is_assignable<AssignTo &, int>::value,
1457 detail::enabler> = detail::dummy>
1458bool lexical_assign(const std::string &input, AssignTo &output) {
1459 if(input.empty()) {
1460 output = 0;
1461 return true;
1462 }
1463 int val{0};
1464 if(lexical_cast(input, val)) {
1465#if defined(__clang__)
1466/* on some older clang compilers */
1467#pragma clang diagnostic push
1468#pragma clang diagnostic ignored "-Wsign-conversion"
1469#elif defined(__GNUC__) && (__GNUC__ == 8)
1470/* gcc 8 warns on intentional assignments such as std::atomic<unsigned long> = int */
1471#pragma GCC diagnostic push
1472#pragma GCC diagnostic ignored "-Wsign-conversion"
1473#endif
1474 output = val;
1475#if defined(__clang__)
1476#pragma clang diagnostic pop
1477#elif defined(__GNUC__) && (__GNUC__ == 8)
1478#pragma GCC diagnostic pop
1479#endif
1480 return true;
1481 }
1482 return false;
1483}
1484
1486template <typename AssignTo,
1487 typename ConvertTo,
1488 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1489 detail::enabler> = detail::dummy>
1490bool lexical_assign(const std::string &input, AssignTo &output) {
1491 ConvertTo val{};
1492 bool parse_result = (!input.empty()) ? lexical_cast(input, val) : true;
1493 if(parse_result) {
1494 output = val;
1495 }
1496 return parse_result;
1497}
1498
1500template <
1501 typename AssignTo,
1502 typename ConvertTo,
1503 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1504 std::is_move_assignable<AssignTo>::value,
1505 detail::enabler> = detail::dummy>
1506bool lexical_assign(const std::string &input, AssignTo &output) {
1507 ConvertTo val{};
1508 bool parse_result = input.empty() ? true : lexical_cast(input, val);
1509 if(parse_result) {
1510 output = AssignTo(val); // use () form of constructor to allow some implicit conversions
1511 }
1512 return parse_result;
1513}
1514
1516template <typename AssignTo,
1517 typename ConvertTo,
1518 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1519 classify_object<AssignTo>::value <= object_category::wrapper_value,
1520 detail::enabler> = detail::dummy>
1521bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1522 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1523}
1524
1527template <typename AssignTo,
1528 typename ConvertTo,
1529 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1530 is_tuple_like<ConvertTo>::value && type_count_base<ConvertTo>::value == 2,
1531 detail::enabler> = detail::dummy>
1532bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1533 // the remove const is to handle pair types coming from a container
1534 using FirstType = typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type;
1535 using SecondType = typename std::tuple_element<1, ConvertTo>::type;
1536 FirstType v1;
1537 SecondType v2{};
1538 bool retval = lexical_assign<FirstType, FirstType>(strings[0], v1);
1539 retval = retval && lexical_assign<SecondType, SecondType>((strings.size() > 1) ? strings[1] : std::string{}, v2);
1540 if(retval) {
1541 output = AssignTo{v1, v2};
1542 }
1543 return retval;
1544}
1545
1547template <class AssignTo,
1548 class ConvertTo,
1549 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1550 type_count<ConvertTo>::value == 1,
1551 detail::enabler> = detail::dummy>
1552bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1553 output.erase(output.begin(), output.end());
1554 if(strings.empty()) {
1555 return true;
1556 }
1557 if(strings.size() == 1 && strings[0] == "{}") {
1558 return true;
1559 }
1560 bool skip_remaining = false;
1561 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1562 skip_remaining = true;
1563 }
1564 for(const auto &elem : strings) {
1565 typename AssignTo::value_type out;
1566 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1567 if(!retval) {
1568 return false;
1569 }
1570 output.insert(output.end(), std::move(out));
1571 if(skip_remaining) {
1572 break;
1573 }
1574 }
1575 return (!output.empty());
1576}
1577
1579template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1580bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1581
1582 if(strings.size() >= 2 && !strings[1].empty()) {
1583 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1584 XC2 x{0.0}, y{0.0};
1585 auto str1 = strings[1];
1586 if(str1.back() == 'i' || str1.back() == 'j') {
1587 str1.pop_back();
1588 }
1589 auto worked = lexical_cast(strings[0], x) && lexical_cast(str1, y);
1590 if(worked) {
1591 output = ConvertTo{x, y};
1592 }
1593 return worked;
1594 }
1595 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1596}
1597
1599template <class AssignTo,
1600 class ConvertTo,
1601 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1602 (type_count<ConvertTo>::value == 1),
1603 detail::enabler> = detail::dummy>
1604bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1605 bool retval = true;
1606 output.clear();
1607 output.reserve(strings.size());
1608 for(const auto &elem : strings) {
1609
1610 output.emplace_back();
1611 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1612 }
1613 return (!output.empty()) && retval;
1614}
1615
1616// forward declaration
1617
1619template <class AssignTo,
1620 class ConvertTo,
1621 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1622 type_count_base<ConvertTo>::value == 2,
1623 detail::enabler> = detail::dummy>
1624bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1625
1627template <class AssignTo,
1628 class ConvertTo,
1629 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1630 type_count_base<ConvertTo>::value != 2 &&
1631 ((type_count<ConvertTo>::value > 2) ||
1632 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1633 detail::enabler> = detail::dummy>
1634bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1635
1637template <class AssignTo,
1638 class ConvertTo,
1639 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1640 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1641 type_count<ConvertTo>::value > 2),
1642 detail::enabler> = detail::dummy>
1643bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output); // forward declaration
1644
1647template <typename AssignTo,
1648 typename ConvertTo,
1649 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1650 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1651 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1652 detail::enabler> = detail::dummy>
1653bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1654
1655 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1656 ConvertTo val;
1657 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1658 output = AssignTo{val};
1659 return retval;
1660 }
1661 output = AssignTo{};
1662 return true;
1663}
1664
1666template <class AssignTo, class ConvertTo, std::size_t I>
1667inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1668tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1669 return true;
1670}
1671
1673template <class AssignTo, class ConvertTo>
1674inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1675tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1676 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1677 strings.erase(strings.begin());
1678 return retval;
1679}
1680
1682template <class AssignTo, class ConvertTo>
1683inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1684 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1685 bool>::type
1686tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1687 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1688 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1689 return retval;
1690}
1691
1693template <class AssignTo, class ConvertTo>
1694inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1695 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1696 bool>::type
1697tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1698
1699 std::size_t index{subtype_count_min<ConvertTo>::value};
1700 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1701 const std::size_t mx{(std::min)(mx_count, strings.size() - 1)};
1702
1703 while(index < mx) {
1704 if(is_separator(strings[index])) {
1705 break;
1706 }
1707 ++index;
1708 }
1709 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1710 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1711 if(strings.size() > index) {
1712 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1713 } else {
1714 strings.clear();
1715 }
1716 return retval;
1717}
1718
1720template <class AssignTo, class ConvertTo, std::size_t I>
1721inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1722tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1723 bool retval = true;
1724 using ConvertToElement = typename std::
1725 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1726 if(!strings.empty()) {
1727 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1728 strings, std::get<I>(output));
1729 }
1730 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1731 return retval;
1732}
1733
1735template <class AssignTo,
1736 class ConvertTo,
1737 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1738 type_count_base<ConvertTo>::value == 2,
1739 detail::enabler>>
1740bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1741 output.clear();
1742 while(!strings.empty()) {
1743
1744 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1{};
1745 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2{};
1746 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1747 if(!strings.empty()) {
1748 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1749 } else {
1750 // an odd number of elements means the second value is missing; never insert a default-constructed v2
1751 retval = false;
1752 }
1753 if(retval) {
1754 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1755 } else {
1756 return false;
1757 }
1758 }
1759 return (!output.empty());
1760}
1761
1763template <class AssignTo,
1764 class ConvertTo,
1765 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1766 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1767 type_count<ConvertTo>::value > 2),
1768 detail::enabler>>
1769bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1770 static_assert(
1771 !is_tuple_like<ConvertTo>::value || type_count_base<AssignTo>::value == type_count_base<ConvertTo>::value,
1772 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1773 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1774}
1775
1777template <class AssignTo,
1778 class ConvertTo,
1779 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1780 type_count_base<ConvertTo>::value != 2 &&
1781 ((type_count<ConvertTo>::value > 2) ||
1782 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1783 detail::enabler>>
1784bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1785 bool retval = true;
1786 output.clear();
1787 std::vector<std::string> temp;
1788 std::size_t ii{0};
1789 std::size_t icount{0};
1790 std::size_t xcm{type_count<ConvertTo>::value};
1791 auto ii_max = strings.size();
1792 while(ii < ii_max) {
1793 temp.push_back(strings[ii]);
1794 ++ii;
1795 ++icount;
1796 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1797 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1798 temp.pop_back();
1799 }
1800 typename AssignTo::value_type temp_out;
1801 retval = retval &&
1802 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1803 temp.clear();
1804 if(!retval) {
1805 return false;
1806 }
1807 output.insert(output.end(), std::move(temp_out));
1808 icount = 0;
1809 }
1810 }
1811 return retval;
1812}
1813
1815template <typename AssignTo,
1816 class ConvertTo,
1817 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1818 std::is_assignable<ConvertTo &, ConvertTo>::value,
1819 detail::enabler> = detail::dummy>
1820bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1821 if(strings.empty() || strings.front().empty()) {
1822 output = ConvertTo{};
1823 return true;
1824 }
1825 typename ConvertTo::value_type val;
1826 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1827 output = ConvertTo{val};
1828 return true;
1829 }
1830 return false;
1831}
1832
1834template <typename AssignTo,
1835 class ConvertTo,
1836 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1837 !std::is_assignable<AssignTo &, ConvertTo>::value,
1838 detail::enabler> = detail::dummy>
1839bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1840 using ConvertType = typename ConvertTo::value_type;
1841 if(strings.empty() || strings.front().empty()) {
1842 output = ConvertType{};
1843 return true;
1844 }
1845 ConvertType val;
1846 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1847 output = val;
1848 return true;
1849 }
1850 return false;
1851}
1852
1854CLI11_INLINE std::string sum_string_vector(const std::vector<std::string> &values);
1855
1856} // namespace detail
1857// [CLI11:type_tools_hpp:end]
1858} // namespace CLI
1859
1860#ifndef CLI11_COMPILE
1861#include "impl/TypeTools_inl.hpp" // IWYU pragma: export
1862#endif
Definition TypeTools.hpp:102
Check for complex.
Definition TypeTools.hpp:241
Definition TypeTools.hpp:183
Check for input streamability.
Definition TypeTools.hpp:230
Definition TypeTools.hpp:219
Definition TypeTools.hpp:305
This can be specialized to override the type deduction for IsMember.
Definition TypeTools.hpp:87
not a pointer
Definition TypeTools.hpp:121
Definition TypeTools.hpp:131
Definition TypeTools.hpp:266
Definition TypeTools.hpp:286
Definition TypeTools.hpp:297
static auto first(Q &&pair_value) -> decltype(std::get< 0 >(std::forward< Q >(pair_value)))
Get the first value (really just the underlying value).
Definition TypeTools.hpp:163
static auto second(Q &&pair_value) -> decltype(std::get< 1 >(std::forward< Q >(pair_value)))
Get the second value (really just the underlying value).
Definition TypeTools.hpp:167
Adaptor for set-like structure: This just wraps a normal container in a few utilities that do almost ...
Definition TypeTools.hpp:136
static auto second(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the second value (really just the underlying value).
Definition TypeTools.hpp:146
static auto first(Q &&pair_value) -> decltype(std::forward< Q >(pair_value))
Get the first value (really just the underlying value).
Definition TypeTools.hpp:142
forward declare the subtype_count_min structure
Definition TypeTools.hpp:526
Set of overloads to get the type size of an object.
Definition TypeTools.hpp:523
This will only trigger for actual void type.
Definition TypeTools.hpp:317
This will only trigger for actual void type.
Definition TypeTools.hpp:529
template to get the underlying value type if it exists or use a default
Definition TypeTools.hpp:511
Check to see if something is bool (fail check by default).
Definition TypeTools.hpp:67
Check to see if something is copyable pointer.
Definition TypeTools.hpp:82
Check to see if something is a shared pointer.
Definition TypeTools.hpp:73
A copy of std::void_t from C++17 (helper for C++11 and C++14).
Definition TypeTools.hpp:56