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