Skip to content

fixing -Wsign-compare warning on GCC #279

@darealshinji

Description

@darealshinji

I get a warning about sign compare:

argparse.hpp: In function ‘std::ostream& argparse::operator<<(std::ostream&, const Argument&)’:
argparse.hpp:686:53: warning: comparison of integer expressions of different signedness: ‘int’ and ‘const std::__cxx11::basic_string<char>::size_type’ {aka ‘const long unsigned int’} [-Wsign-compare]
  686 |     while ((pos = argument.m_help.find('\n', prev)) != std::string::npos) {
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~

This is because at line 680 auto pos is recognized as signed int instead of an unsigned type.
I recommend marking the values of pos and prev as unsigned or setting their type explicitly to size_t:

    auto pos = 0u;
    auto prev = 0u;
    size_t pos = 0;
    size_t prev = 0;

See also the comments in this PR: #276 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions