Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing -Wsign-compare warning on GCC #279

Closed
darealshinji opened this issue Aug 4, 2023 · 2 comments
Closed

fixing -Wsign-compare warning on GCC #279

darealshinji opened this issue Aug 4, 2023 · 2 comments

Comments

@darealshinji
Copy link

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)

@darealshinji
Copy link
Author

darealshinji commented Aug 4, 2023

Forget that fix. After that -h and --help and automatic help messages don't work anymore. This might explain #242

I've changed it like this:

    long pos = 0;
    long prev = 0;
    auto first_line = true;
    auto hspace = "  "; // minimal space between name and help message
    stream << name_stream.str();
    std::string_view help_view(argument.m_help);
    while ((pos = argument.m_help.find('\n', prev)) != (long)std::string::npos) {

@p-ranav
Copy link
Owner

p-ranav commented Oct 27, 2023

Fixed by #292

@p-ranav p-ranav closed this as completed Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants