Comment

Comment by MathMonkeyMan

You returned a pointer to a temporary. It's nasty but easy to spot.

Would you return `input.c_str()`? You might think twice about that, but it's the same thing. My favorite fix is to take a `std::string_view` as the parameter.

    string_view remove_leading_spaces(string_view input) {
        auto new_start = find_if(input.begin(), input.end(), [](char ch) { return ch != ' '; });
        return string_view(new_start, input.end());
    }
The result of the undefined behavior in your program is surprising, though. As it usually is.

No comments yet

Be the first to share your thoughts.

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton