From d8453810286466c3622168f4041ce041e3f66aae Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Thu, 5 Jan 2023 12:08:03 -0700 Subject: [PATCH] Fix Argument bool bit fields The intent of ": 1" is to use individual bits to store the bool state of these class values. Because true != 0, this worked. But it was likely to bite someone sometime. (My bad: 0fe17e22f6.) This commit also adds m_accepts_optional_like_value to the bit field and sets the default false value in the constructor. Because we cannot set a default value during declaration (until C++20). make sure future coders know to set the preferred default in the constructor. Closes #213 Signed-off-by: Sean Robinson --- include/argparse/argparse.hpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index d0ce31d..ed02ddf 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -376,7 +376,8 @@ class Argument { explicit Argument(std::string_view prefix_chars, std::array &&a, std::index_sequence /*unused*/) - : m_is_optional((is_optional(a[I], prefix_chars) || ...)), + : m_accepts_optional_like_value(false), + m_is_optional((is_optional(a[I], prefix_chars) || ...)), m_is_required(false), m_is_repeatable(false), m_is_used(false), m_prefix_chars(prefix_chars) { ((void)m_names.emplace_back(a[I]), ...); @@ -1033,11 +1034,12 @@ private: [](const std::string &value) { return value; }}; std::vector m_values; NArgsRange m_num_args_range{1, 1}; - bool m_accepts_optional_like_value = false; - bool m_is_optional : true; - bool m_is_required : true; - bool m_is_repeatable : true; - bool m_is_used : true; // True if the optional argument is used by user + // Bit field of bool values. Set default value in ctor. + bool m_accepts_optional_like_value : 1; + bool m_is_optional : 1; + bool m_is_required : 1; + bool m_is_repeatable : 1; + bool m_is_used : 1; std::string_view m_prefix_chars; // ArgumentParser has the prefix_chars };