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. Check the actual numeric
976 // payload, including when an explicit octal or binary prefix is removed before conversion.
977 const bool has_base_prefix = input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0 ||
978 input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0;
979 auto first_non_ws = input.find_first_not_of(" \t\n\v\f\r", has_base_prefix ? 2 : 0);
980 if(first_non_ws != std::string::npos && input[first_non_ws] == '-') {
981 return false;
982 }
983 char *val{nullptr};
984 errno = 0;
985 std::uint64_t output_ll = std::strtoull(input.c_str(), &val, 0);
986 if(errno == ERANGE) {
987 return false;
988 }
989 output = static_cast<T>(output_ll);
990 if(val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll) {
991 return true;
992 }
993 val = nullptr;
994 std::int64_t output_sll = std::strtoll(input.c_str(), &val, 0);
995 if(val == (input.c_str() + input.size())) {
996 output = (output_sll < 0) ? static_cast<T>(0) : static_cast<T>(output_sll);
997 return (static_cast<std::int64_t>(output) == output_sll);
998 }
999 // remove separators if present
1000 auto group_separators = get_group_separators();
1001 if(input.find_first_of(group_separators) != std::string::npos) {
1002 std::string nstring = input;
1003 for(auto &separator : group_separators) {
1004 if(input.find_first_of(separator) != std::string::npos) {
1005 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
1006 }
1007 }
1008 return integral_conversion(nstring, output);
1009 }
1010
1011 if(std::isspace(static_cast<unsigned char>(input.back()))) {
1012 return integral_conversion(trim_copy(input), output);
1013 }
1014 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
1015 val = nullptr;
1016 errno = 0;
1017 output_ll = std::strtoull(input.c_str() + 2, &val, 8);
1018 if(errno == ERANGE) {
1019 return false;
1020 }
1021 output = static_cast<T>(output_ll);
1022 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
1023 }
1024 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
1025 // LCOV_EXCL_START
1026 // In some new compilers including the coverage testing one binary strings are handled properly in strtoull
1027 // automatically so this coverage is missing but is well tested in other compilers
1028 val = nullptr;
1029 errno = 0;
1030 output_ll = std::strtoull(input.c_str() + 2, &val, 2);
1031 if(errno == ERANGE) {
1032 return false;
1033 }
1034 output = static_cast<T>(output_ll);
1035 return (val == (input.c_str() + input.size()) && static_cast<std::uint64_t>(output) == output_ll);
1036 // LCOV_EXCL_STOP
1037 }
1038 return false;
1039}
1040
1042template <typename T, enable_if_t<std::is_signed<T>::value, detail::enabler> = detail::dummy>
1043bool integral_conversion(const std::string &input, T &output) noexcept {
1044 if(input.empty()) {
1045 return false;
1046 }
1047 char *val = nullptr;
1048 errno = 0;
1049 std::int64_t output_ll = std::strtoll(input.c_str(), &val, 0);
1050 if(errno == ERANGE) {
1051 return false;
1052 }
1053 output = static_cast<T>(output_ll);
1054 if(val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll) {
1055 return true;
1056 }
1057 if(input == "true") {
1058 // this is to deal with a few oddities with flags and wrapper int types
1059 output = static_cast<T>(1);
1060 return true;
1061 }
1062 // remove separators if present
1063 auto group_separators = get_group_separators();
1064 if(input.find_first_of(group_separators) != std::string::npos) {
1065 for(auto &separator : group_separators) {
1066 if(input.find_first_of(separator) != std::string::npos) {
1067 std::string nstring = input;
1068 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
1069 return integral_conversion(nstring, output);
1070 }
1071 }
1072 }
1073 if(std::isspace(static_cast<unsigned char>(input.back()))) {
1074 return integral_conversion(trim_copy(input), output);
1075 }
1076 if(input.compare(0, 2, "0o") == 0 || input.compare(0, 2, "0O") == 0) {
1077 val = nullptr;
1078 errno = 0;
1079 output_ll = std::strtoll(input.c_str() + 2, &val, 8);
1080 if(errno == ERANGE) {
1081 return false;
1082 }
1083 output = static_cast<T>(output_ll);
1084 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1085 }
1086 if(input.compare(0, 2, "0b") == 0 || input.compare(0, 2, "0B") == 0) {
1087 // LCOV_EXCL_START
1088 // In some new compilers including the coverage testing one binary strings are handled properly in strtoll
1089 // automatically so this coverage is missing but is well tested in other compilers
1090 val = nullptr;
1091 errno = 0;
1092 output_ll = std::strtoll(input.c_str() + 2, &val, 2);
1093 if(errno == ERANGE) {
1094 return false;
1095 }
1096 output = static_cast<T>(output_ll);
1097 return (val == (input.c_str() + input.size()) && static_cast<std::int64_t>(output) == output_ll);
1098 // LCOV_EXCL_STOP
1099 }
1100 return false;
1101}
1102
1104CLI11_INLINE std::int64_t to_flag_value(std::string val) noexcept;
1105
1107template <typename T,
1108 enable_if_t<classify_object<T>::value == object_category::integral_value ||
1109 classify_object<T>::value == object_category::unsigned_integral,
1110 detail::enabler> = detail::dummy>
1111bool lexical_cast(const std::string &input, T &output) {
1112 return integral_conversion(input, output);
1113}
1114
1116template <typename T,
1117 enable_if_t<classify_object<T>::value == object_category::char_value, detail::enabler> = detail::dummy>
1118bool lexical_cast(const std::string &input, T &output) {
1119 if(input.size() == 1) {
1120 output = static_cast<T>(input[0]);
1121 return true;
1122 }
1123 std::int8_t res{0};
1124 // we do it this way as some systems have char as signed and not, this ensures consistency in the way things are
1125 // handled
1126 bool result = integral_conversion(input, res);
1127 if(result) {
1128 output = static_cast<T>(res);
1129 }
1130 return result;
1131}
1132
1134template <typename T,
1135 enable_if_t<classify_object<T>::value == object_category::boolean_value, detail::enabler> = detail::dummy>
1136bool lexical_cast(const std::string &input, T &output) {
1137 errno = 0;
1138 auto out = to_flag_value(input);
1139 if(errno == 0) {
1140 output = (out > 0);
1141 } else if(errno == ERANGE) {
1142 output = (input[0] != '-');
1143 } else {
1144 return false;
1145 }
1146 return true;
1147}
1148
1150template <typename T,
1151 enable_if_t<classify_object<T>::value == object_category::floating_point, detail::enabler> = detail::dummy>
1152bool lexical_cast(const std::string &input, T &output) {
1153 if(input.empty()) {
1154 return false;
1155 }
1156 char *val = nullptr;
1157 auto output_ld = std::strtold(input.c_str(), &val);
1158 // strtold performs no conversion (and leaves val == start) for inputs like whitespace-only strings;
1159 // treat that as a failure rather than reporting a successful conversion to 0
1160 if(val == input.c_str()) {
1161 return false;
1162 }
1163 output = static_cast<T>(output_ld);
1164 if(val == (input.c_str() + input.size())) {
1165 return true;
1166 }
1167 while(std::isspace(static_cast<unsigned char>(*val))) {
1168 ++val;
1169 if(val == (input.c_str() + input.size())) {
1170 return true;
1171 }
1172 }
1173
1174 // remove separators if present
1175 auto group_separators = get_group_separators();
1176 if(input.find_first_of(group_separators) != std::string::npos) {
1177 for(auto &separator : group_separators) {
1178 if(input.find_first_of(separator) != std::string::npos) {
1179 std::string nstring = input;
1180 nstring.erase(std::remove(nstring.begin(), nstring.end(), separator), nstring.end());
1181 return lexical_cast(nstring, output);
1182 }
1183 }
1184 }
1185 return false;
1186}
1187
1189template <typename T,
1190 enable_if_t<classify_object<T>::value == object_category::complex_number, detail::enabler> = detail::dummy>
1191bool lexical_cast(const std::string &input, T &output) {
1192 using XC = typename wrapped_type<T, double>::type;
1193 XC x{0.0}, y{0.0};
1194 auto str1 = input;
1195 bool worked = false;
1196 auto nloc = str1.find_last_of("+-");
1197 if(nloc != std::string::npos && nloc > 0) {
1198 worked = lexical_cast(str1.substr(0, nloc), x);
1199 str1 = str1.substr(nloc);
1200 if(str1.back() == 'i' || str1.back() == 'j')
1201 str1.pop_back();
1202 worked = worked && lexical_cast(str1, y);
1203 } else {
1204 if(str1.back() == 'i' || str1.back() == 'j') {
1205 str1.pop_back();
1206 worked = lexical_cast(str1, y);
1207 x = XC{0};
1208 } else {
1209 worked = lexical_cast(str1, x);
1210 y = XC{0};
1211 }
1212 }
1213 if(worked) {
1214 output = T{x, y};
1215 return worked;
1216 }
1217 return from_stream(input, output);
1218}
1219
1221template <typename T,
1222 enable_if_t<classify_object<T>::value == object_category::string_assignable, detail::enabler> = detail::dummy>
1223bool lexical_cast(const std::string &input, T &output) {
1224 output = input;
1225 return true;
1226}
1227
1229template <
1230 typename T,
1231 enable_if_t<classify_object<T>::value == object_category::string_constructible, detail::enabler> = detail::dummy>
1232bool lexical_cast(const std::string &input, T &output) {
1233 output = T(input);
1234 return true;
1235}
1236
1238template <
1239 typename T,
1240 enable_if_t<classify_object<T>::value == object_category::wstring_assignable, detail::enabler> = detail::dummy>
1241bool lexical_cast(const std::string &input, T &output) {
1242 output = widen(input);
1243 return true;
1244}
1245
1246template <
1247 typename T,
1248 enable_if_t<classify_object<T>::value == object_category::wstring_constructible, detail::enabler> = detail::dummy>
1249bool lexical_cast(const std::string &input, T &output) {
1250 output = T{widen(input)};
1251 return true;
1252}
1253
1255template <typename T,
1256 enable_if_t<classify_object<T>::value == object_category::enumeration, detail::enabler> = detail::dummy>
1257bool lexical_cast(const std::string &input, T &output) {
1258 typename std::underlying_type<T>::type val;
1259 if(!integral_conversion(input, val)) {
1260 return false;
1261 }
1262 output = static_cast<T>(val);
1263 return true;
1264}
1265
1267template <typename T,
1268 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1269 std::is_assignable<T &, typename T::value_type>::value,
1270 detail::enabler> = detail::dummy>
1271bool lexical_cast(const std::string &input, T &output) {
1272 typename T::value_type val;
1273 if(lexical_cast(input, val)) {
1274 output = val;
1275 return true;
1276 }
1277 return from_stream(input, output);
1278}
1279
1280template <typename T,
1281 enable_if_t<classify_object<T>::value == object_category::wrapper_value &&
1282 !std::is_assignable<T &, typename T::value_type>::value && std::is_assignable<T &, T>::value,
1283 detail::enabler> = detail::dummy>
1284bool lexical_cast(const std::string &input, T &output) {
1285 typename T::value_type val;
1286 if(lexical_cast(input, val)) {
1287 output = T{val};
1288 return true;
1289 }
1290 return from_stream(input, output);
1291}
1292
1294template <
1295 typename T,
1296 enable_if_t<classify_object<T>::value == object_category::number_constructible, detail::enabler> = detail::dummy>
1297bool lexical_cast(const std::string &input, T &output) {
1298 int val = 0;
1299 if(integral_conversion(input, val)) {
1300 output = T(val);
1301 return true;
1302 }
1303
1304 double dval = 0.0;
1305 if(lexical_cast(input, dval)) {
1306 output = T{dval};
1307 return true;
1308 }
1309
1310 return from_stream(input, output);
1311}
1312
1314template <
1315 typename T,
1316 enable_if_t<classify_object<T>::value == object_category::integer_constructible, detail::enabler> = detail::dummy>
1317bool lexical_cast(const std::string &input, T &output) {
1318 int val = 0;
1319 if(integral_conversion(input, val)) {
1320 output = T(val);
1321 return true;
1322 }
1323 return from_stream(input, output);
1324}
1325
1327template <
1328 typename T,
1329 enable_if_t<classify_object<T>::value == object_category::double_constructible, detail::enabler> = detail::dummy>
1330bool lexical_cast(const std::string &input, T &output) {
1331 double val = 0.0;
1332 if(lexical_cast(input, val)) {
1333 output = T{val};
1334 return true;
1335 }
1336 return from_stream(input, output);
1337}
1338
1340template <typename T,
1341 enable_if_t<classify_object<T>::value == object_category::other && std::is_assignable<T &, int>::value,
1342 detail::enabler> = detail::dummy>
1343bool lexical_cast(const std::string &input, T &output) {
1344 int val = 0;
1345 if(integral_conversion(input, val)) {
1346#ifdef _MSC_VER
1347#pragma warning(push)
1348#pragma warning(disable : 4800)
1349#endif
1350 // with Atomic<XX> this could produce a warning due to the conversion but if atomic gets here it is an old style
1351 // so will most likely still work
1352 output = val;
1353#ifdef _MSC_VER
1354#pragma warning(pop)
1355#endif
1356 return true;
1357 }
1358 // LCOV_EXCL_START
1359 // This version of cast is only used for odd cases in an older compilers the fail over
1360 // from_stream is tested elsewhere an not relevant for coverage here
1361 return from_stream(input, output);
1362 // LCOV_EXCL_STOP
1363}
1364
1366template <typename T,
1367 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1368 is_istreamable<T>::value,
1369 detail::enabler> = detail::dummy>
1370bool lexical_cast(const std::string &input, T &output) {
1371 return from_stream(input, output);
1372}
1373
1376template <typename T,
1377 enable_if_t<classify_object<T>::value == object_category::other && !std::is_assignable<T &, int>::value &&
1378 !is_istreamable<T>::value && !adl_detail::is_lexical_castable<T>::value,
1379 detail::enabler> = detail::dummy>
1380bool lexical_cast(const std::string & /*input*/, T & /*output*/) {
1381 static_assert(!std::is_same<T, T>::value, // Can't just write false here.
1382 "option object type must have a lexical cast overload or streaming input operator(>>) defined, if it "
1383 "is convertible from another type use the add_option<T, XC>(...) with XC being the known type");
1384 return false;
1385}
1386
1389template <typename AssignTo,
1390 typename ConvertTo,
1391 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !is_wrapper<AssignTo>::value &&
1392 (classify_object<AssignTo>::value == object_category::string_assignable ||
1393 classify_object<AssignTo>::value == object_category::string_constructible ||
1394 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1395 classify_object<AssignTo>::value == object_category::wstring_constructible),
1396 detail::enabler> = detail::dummy>
1397bool lexical_assign(const std::string &input, AssignTo &output) {
1398 return lexical_cast(input, output);
1399}
1400
1403template <typename AssignTo,
1404 typename ConvertTo,
1405 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && is_wrapper<AssignTo>::value &&
1406 (classify_object<AssignTo>::value == object_category::string_assignable ||
1407 classify_object<AssignTo>::value == object_category::string_constructible ||
1408 classify_object<AssignTo>::value == object_category::wstring_assignable ||
1409 classify_object<AssignTo>::value == object_category::wstring_constructible),
1410 detail::enabler> = detail::dummy>
1411bool lexical_assign(const std::string &input, AssignTo &output) {
1412 if(input.empty()) {
1413 output = AssignTo{};
1414 return true;
1415 }
1416 return lexical_cast(input, output);
1417}
1418
1420template <typename AssignTo,
1421 typename ConvertTo,
1422 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, AssignTo>::value &&
1423 classify_object<AssignTo>::value != object_category::string_assignable &&
1424 classify_object<AssignTo>::value != object_category::string_constructible &&
1425 classify_object<AssignTo>::value != object_category::wstring_assignable &&
1426 classify_object<AssignTo>::value != object_category::wstring_constructible,
1427 detail::enabler> = detail::dummy>
1428bool lexical_assign(const std::string &input, AssignTo &output) {
1429 if(input.empty()) {
1430 output = AssignTo{};
1431 return true;
1432 }
1433
1434 return lexical_cast(input, output);
1435} // LCOV_EXCL_LINE
1436
1438template <typename AssignTo,
1439 typename ConvertTo,
1440 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1441 classify_object<AssignTo>::value == object_category::wrapper_value,
1442 detail::enabler> = detail::dummy>
1443bool lexical_assign(const std::string &input, AssignTo &output) {
1444 if(input.empty()) {
1445 typename AssignTo::value_type emptyVal{};
1446 output = emptyVal;
1447 return true;
1448 }
1449 return lexical_cast(input, output);
1450}
1451
1454template <typename AssignTo,
1455 typename ConvertTo,
1456 enable_if_t<std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, AssignTo>::value &&
1457 classify_object<AssignTo>::value != object_category::wrapper_value &&
1458 std::is_assignable<AssignTo &, int>::value,
1459 detail::enabler> = detail::dummy>
1460bool lexical_assign(const std::string &input, AssignTo &output) {
1461 if(input.empty()) {
1462 output = 0;
1463 return true;
1464 }
1465 int val{0};
1466 if(lexical_cast(input, val)) {
1467#if defined(__clang__)
1468/* on some older clang compilers */
1469#pragma clang diagnostic push
1470#pragma clang diagnostic ignored "-Wsign-conversion"
1471#elif defined(__GNUC__) && (__GNUC__ == 8)
1472/* gcc 8 warns on intentional assignments such as std::atomic<unsigned long> = int */
1473#pragma GCC diagnostic push
1474#pragma GCC diagnostic ignored "-Wsign-conversion"
1475#endif
1476 output = val;
1477#if defined(__clang__)
1478#pragma clang diagnostic pop
1479#elif defined(__GNUC__) && (__GNUC__ == 8)
1480#pragma GCC diagnostic pop
1481#endif
1482 return true;
1483 }
1484 return false;
1485}
1486
1488template <typename AssignTo,
1489 typename ConvertTo,
1490 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && std::is_assignable<AssignTo &, ConvertTo &>::value,
1491 detail::enabler> = detail::dummy>
1492bool lexical_assign(const std::string &input, AssignTo &output) {
1493 ConvertTo val{};
1494 bool parse_result = (!input.empty()) ? lexical_cast(input, val) : true;
1495 if(parse_result) {
1496 output = val;
1497 }
1498 return parse_result;
1499}
1500
1502template <
1503 typename AssignTo,
1504 typename ConvertTo,
1505 enable_if_t<!std::is_same<AssignTo, ConvertTo>::value && !std::is_assignable<AssignTo &, ConvertTo &>::value &&
1506 std::is_move_assignable<AssignTo>::value,
1507 detail::enabler> = detail::dummy>
1508bool lexical_assign(const std::string &input, AssignTo &output) {
1509 ConvertTo val{};
1510 bool parse_result = input.empty() ? true : lexical_cast(input, val);
1511 if(parse_result) {
1512 output = AssignTo(val); // use () form of constructor to allow some implicit conversions
1513 }
1514 return parse_result;
1515}
1516
1518template <typename AssignTo,
1519 typename ConvertTo,
1520 enable_if_t<classify_object<ConvertTo>::value <= object_category::other &&
1521 classify_object<AssignTo>::value <= object_category::wrapper_value,
1522 detail::enabler> = detail::dummy>
1523bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1524 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1525}
1526
1529template <typename AssignTo,
1530 typename ConvertTo,
1531 enable_if_t<(type_count<AssignTo>::value <= 2) && expected_count<AssignTo>::value == 1 &&
1532 is_tuple_like<ConvertTo>::value && type_count_base<ConvertTo>::value == 2,
1533 detail::enabler> = detail::dummy>
1534bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1535 // the remove const is to handle pair types coming from a container
1536 using FirstType = typename std::remove_const<typename std::tuple_element<0, ConvertTo>::type>::type;
1537 using SecondType = typename std::tuple_element<1, ConvertTo>::type;
1538 FirstType v1;
1539 SecondType v2{};
1540 bool retval = lexical_assign<FirstType, FirstType>(strings[0], v1);
1541 retval = retval && lexical_assign<SecondType, SecondType>((strings.size() > 1) ? strings[1] : std::string{}, v2);
1542 if(retval) {
1543 output = AssignTo{v1, v2};
1544 }
1545 return retval;
1546}
1547
1549template <class AssignTo,
1550 class ConvertTo,
1551 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1552 type_count<ConvertTo>::value == 1,
1553 detail::enabler> = detail::dummy>
1554bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1555 output.erase(output.begin(), output.end());
1556 if(strings.empty()) {
1557 return true;
1558 }
1559 if(strings.size() == 1 && strings[0] == "{}") {
1560 return true;
1561 }
1562 bool skip_remaining = false;
1563 if(strings.size() == 2 && strings[0] == "{}" && is_separator(strings[1])) {
1564 skip_remaining = true;
1565 }
1566 for(const auto &elem : strings) {
1567 typename AssignTo::value_type out;
1568 bool retval = lexical_assign<typename AssignTo::value_type, typename ConvertTo::value_type>(elem, out);
1569 if(!retval) {
1570 return false;
1571 }
1572 output.insert(output.end(), std::move(out));
1573 if(skip_remaining) {
1574 break;
1575 }
1576 }
1577 return (!output.empty());
1578}
1579
1581template <class AssignTo, class ConvertTo, enable_if_t<is_complex<ConvertTo>::value, detail::enabler> = detail::dummy>
1582bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1583
1584 if(strings.size() >= 2 && !strings[1].empty()) {
1585 using XC2 = typename wrapped_type<ConvertTo, double>::type;
1586 XC2 x{0.0}, y{0.0};
1587 auto str1 = strings[1];
1588 if(str1.back() == 'i' || str1.back() == 'j') {
1589 str1.pop_back();
1590 }
1591 auto worked = lexical_cast(strings[0], x) && lexical_cast(str1, y);
1592 if(worked) {
1593 output = ConvertTo{x, y};
1594 }
1595 return worked;
1596 }
1597 return lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1598}
1599
1601template <class AssignTo,
1602 class ConvertTo,
1603 enable_if_t<is_mutable_container<AssignTo>::value && (expected_count<ConvertTo>::value == 1) &&
1604 (type_count<ConvertTo>::value == 1),
1605 detail::enabler> = detail::dummy>
1606bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1607 bool retval = true;
1608 output.clear();
1609 output.reserve(strings.size());
1610 for(const auto &elem : strings) {
1611
1612 output.emplace_back();
1613 retval = retval && lexical_assign<typename AssignTo::value_type, ConvertTo>(elem, output.back());
1614 }
1615 return (!output.empty()) && retval;
1616}
1617
1618// forward declaration
1619
1621template <class AssignTo,
1622 class ConvertTo,
1623 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1624 type_count_base<ConvertTo>::value == 2,
1625 detail::enabler> = detail::dummy>
1626bool lexical_conversion(std::vector<std::string> strings, AssignTo &output);
1627
1629template <class AssignTo,
1630 class ConvertTo,
1631 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1632 type_count_base<ConvertTo>::value != 2 &&
1633 ((type_count<ConvertTo>::value > 2) ||
1634 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1635 detail::enabler> = detail::dummy>
1636bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output);
1637
1639template <class AssignTo,
1640 class ConvertTo,
1641 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1642 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1643 type_count<ConvertTo>::value > 2),
1644 detail::enabler> = detail::dummy>
1645bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output); // forward declaration
1646
1649template <typename AssignTo,
1650 typename ConvertTo,
1651 enable_if_t<!is_tuple_like<AssignTo>::value && !is_mutable_container<AssignTo>::value &&
1652 classify_object<ConvertTo>::value != object_category::wrapper_value &&
1653 (is_mutable_container<ConvertTo>::value || type_count<ConvertTo>::value > 2),
1654 detail::enabler> = detail::dummy>
1655bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1656
1657 if(strings.size() > 1 || (!strings.empty() && !(strings.front().empty()))) {
1658 ConvertTo val;
1659 auto retval = lexical_conversion<ConvertTo, ConvertTo>(strings, val);
1660 output = AssignTo{val};
1661 return retval;
1662 }
1663 output = AssignTo{};
1664 return true;
1665}
1666
1668template <class AssignTo, class ConvertTo, std::size_t I>
1669inline typename std::enable_if<(I >= type_count_base<AssignTo>::value), bool>::type
1670tuple_conversion(const std::vector<std::string> &, AssignTo &) {
1671 return true;
1672}
1673
1675template <class AssignTo, class ConvertTo>
1676inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && type_count<ConvertTo>::value == 1, bool>::type
1677tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1678 auto retval = lexical_assign<AssignTo, ConvertTo>(strings[0], output);
1679 strings.erase(strings.begin());
1680 return retval;
1681}
1682
1684template <class AssignTo, class ConvertTo>
1685inline typename std::enable_if<!is_mutable_container<ConvertTo>::value && (type_count<ConvertTo>::value > 1) &&
1686 type_count<ConvertTo>::value == type_count_min<ConvertTo>::value,
1687 bool>::type
1688tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1689 auto retval = lexical_conversion<AssignTo, ConvertTo>(strings, output);
1690 strings.erase(strings.begin(), strings.begin() + type_count<ConvertTo>::value);
1691 return retval;
1692}
1693
1695template <class AssignTo, class ConvertTo>
1696inline typename std::enable_if<is_mutable_container<ConvertTo>::value ||
1697 type_count<ConvertTo>::value != type_count_min<ConvertTo>::value,
1698 bool>::type
1699tuple_type_conversion(std::vector<std::string> &strings, AssignTo &output) {
1700
1701 std::size_t index{subtype_count_min<ConvertTo>::value};
1702 const std::size_t mx_count{subtype_count<ConvertTo>::value};
1703 const std::size_t mx{(std::min)(mx_count, strings.size() - 1)};
1704
1705 while(index < mx) {
1706 if(is_separator(strings[index])) {
1707 break;
1708 }
1709 ++index;
1710 }
1711 bool retval = lexical_conversion<AssignTo, ConvertTo>(
1712 std::vector<std::string>(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index)), output);
1713 if(strings.size() > index) {
1714 strings.erase(strings.begin(), strings.begin() + static_cast<std::ptrdiff_t>(index) + 1);
1715 } else {
1716 strings.clear();
1717 }
1718 return retval;
1719}
1720
1722template <class AssignTo, class ConvertTo, std::size_t I>
1723inline typename std::enable_if<(I < type_count_base<AssignTo>::value), bool>::type
1724tuple_conversion(std::vector<std::string> strings, AssignTo &output) {
1725 bool retval = true;
1726 using ConvertToElement = typename std::
1727 conditional<is_tuple_like<ConvertTo>::value, typename std::tuple_element<I, ConvertTo>::type, ConvertTo>::type;
1728 if(!strings.empty()) {
1729 retval = retval && tuple_type_conversion<typename std::tuple_element<I, AssignTo>::type, ConvertToElement>(
1730 strings, std::get<I>(output));
1731 }
1732 retval = retval && tuple_conversion<AssignTo, ConvertTo, I + 1>(std::move(strings), output);
1733 return retval;
1734}
1735
1737template <class AssignTo,
1738 class ConvertTo,
1739 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1740 type_count_base<ConvertTo>::value == 2,
1741 detail::enabler>>
1742bool lexical_conversion(std::vector<std::string> strings, AssignTo &output) {
1743 output.clear();
1744 while(!strings.empty()) {
1745
1746 typename std::remove_const<typename std::tuple_element<0, typename ConvertTo::value_type>::type>::type v1{};
1747 typename std::tuple_element<1, typename ConvertTo::value_type>::type v2{};
1748 bool retval = tuple_type_conversion<decltype(v1), decltype(v1)>(strings, v1);
1749 if(!strings.empty()) {
1750 retval = retval && tuple_type_conversion<decltype(v2), decltype(v2)>(strings, v2);
1751 } else {
1752 // an odd number of elements means the second value is missing; never insert a default-constructed v2
1753 retval = false;
1754 }
1755 if(retval) {
1756 output.insert(output.end(), typename AssignTo::value_type{v1, v2});
1757 } else {
1758 return false;
1759 }
1760 }
1761 return (!output.empty());
1762}
1763
1765template <class AssignTo,
1766 class ConvertTo,
1767 enable_if_t<is_tuple_like<AssignTo>::value && is_tuple_like<ConvertTo>::value &&
1768 (type_count_base<ConvertTo>::value != type_count<ConvertTo>::value ||
1769 type_count<ConvertTo>::value > 2),
1770 detail::enabler>>
1771bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1772 static_assert(
1773 !is_tuple_like<ConvertTo>::value || type_count_base<AssignTo>::value == type_count_base<ConvertTo>::value,
1774 "if the conversion type is defined as a tuple it must be the same size as the type you are converting to");
1775 return tuple_conversion<AssignTo, ConvertTo, 0>(strings, output);
1776}
1777
1779template <class AssignTo,
1780 class ConvertTo,
1781 enable_if_t<is_mutable_container<AssignTo>::value && is_mutable_container<ConvertTo>::value &&
1782 type_count_base<ConvertTo>::value != 2 &&
1783 ((type_count<ConvertTo>::value > 2) ||
1784 (type_count<ConvertTo>::value > type_count_base<ConvertTo>::value)),
1785 detail::enabler>>
1786bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1787 bool retval = true;
1788 output.clear();
1789 std::vector<std::string> temp;
1790 std::size_t ii{0};
1791 std::size_t icount{0};
1792 std::size_t xcm{type_count<ConvertTo>::value};
1793 auto ii_max = strings.size();
1794 while(ii < ii_max) {
1795 temp.push_back(strings[ii]);
1796 ++ii;
1797 ++icount;
1798 if(icount == xcm || is_separator(temp.back()) || ii == ii_max) {
1799 if(static_cast<int>(xcm) > type_count_min<ConvertTo>::value && is_separator(temp.back())) {
1800 temp.pop_back();
1801 }
1802 typename AssignTo::value_type temp_out;
1803 retval = retval &&
1804 lexical_conversion<typename AssignTo::value_type, typename ConvertTo::value_type>(temp, temp_out);
1805 temp.clear();
1806 if(!retval) {
1807 return false;
1808 }
1809 output.insert(output.end(), std::move(temp_out));
1810 icount = 0;
1811 }
1812 }
1813 return retval;
1814}
1815
1817template <typename AssignTo,
1818 class ConvertTo,
1819 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1820 std::is_assignable<ConvertTo &, ConvertTo>::value,
1821 detail::enabler> = detail::dummy>
1822bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1823 if(strings.empty() || strings.front().empty()) {
1824 output = ConvertTo{};
1825 return true;
1826 }
1827 typename ConvertTo::value_type val;
1828 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1829 output = ConvertTo{val};
1830 return true;
1831 }
1832 return false;
1833}
1834
1836template <typename AssignTo,
1837 class ConvertTo,
1838 enable_if_t<classify_object<ConvertTo>::value == object_category::wrapper_value &&
1839 !std::is_assignable<AssignTo &, ConvertTo>::value,
1840 detail::enabler> = detail::dummy>
1841bool lexical_conversion(const std::vector<std::string> &strings, AssignTo &output) {
1842 using ConvertType = typename ConvertTo::value_type;
1843 if(strings.empty() || strings.front().empty()) {
1844 output = ConvertType{};
1845 return true;
1846 }
1847 ConvertType val;
1848 if(lexical_conversion<typename ConvertTo::value_type, typename ConvertTo::value_type>(strings, val)) {
1849 output = val;
1850 return true;
1851 }
1852 return false;
1853}
1854
1856CLI11_INLINE std::string sum_string_vector(const std::vector<std::string> &values);
1857
1858} // namespace detail
1859// [CLI11:type_tools_hpp:end]
1860} // namespace CLI
1861
1862#ifndef CLI11_COMPILE
1863#include "impl/TypeTools_inl.hpp" // IWYU pragma: export
1864#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