Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ class Argument {
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)) != std::string::npos) {
while ((pos = argument.m_help.find('\n', prev)) != (signed)std::string::npos) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of c-style casting npos to signed I would change auto pos = 0 to size_t pos = 0. The root problem is that "pos" is being auto-typed to an int whereas the return type of string::find() and the type of string::npos are both size_t.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also gets auto-typed correctly if you mark the zeros as unsigned:

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I resolved/suppressed the warning by using a static_cast, my minimal reasoning was that the std::string::npos will always be a signed int (of -1) according to the std::string::npos https://cplusplus.com/reference/string/string/npos/
while ((pos = argument.m_help.find('\n', prev)) != static_cast<int>(std::string::npos)) {

auto line = help_view.substr(prev, pos - prev + 1);
if (first_line) {
stream << hspace << line;
Expand Down