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