C++ split function (equivalent to python string.split()) I wrote the equivalent to the split() in python for C++.  I would like to know how I can make it better:

static constexpr auto whitespace = " \t\v\r\n\f";
static constexpr auto npos = std::string::npos;

std::vector&lt;std::string&gt; split(std::string_view str, std::string_view sep = "", std::size_t maxsplit = std::numeric_limits&lt;std::size_t&gt;::max())
{
    std::vector&lt;std::string&gt; result;
    if (sep.empty())
    {
        for (std::size_t start = str.find_first_not_of(whitespace), splits = 0; start != npos; ++splits)
        {
            if (auto end = str.find_first_of(whitespace, start); end != npos &amp;&amp; splits &lt; maxsplit)
            {
                result.emplace_back(str.substr(start, end - start));
                start = str.find_first_not_of(whitespace, end);
            }
            else
            {
                result.emplace_back(str.substr(start));
                break;
            }
        }
    }
    else
    {
        for (std::size_t start = 0, splits = 0; start &lt; str.size(); ++splits)
        {
            if (auto end = str.find(sep, start); end != npos &amp;&amp; splits &lt; maxsplit)
            {
                result.emplace_back(str.substr(start, end - start));
                start = end + sep.size();
            }
            else
            {
                result.emplace_back(str.substr(start));
                break;
            }
        }
    }
    return result;
}


I tried to keep all variables as local as possible and use standard algorithms.
I would use a const reference here:

static constexpr auto npos = std::string::npos;

// I would do this.
static constexpr auto const&amp; npos = std::string::npos;


No point in duplicating storage.
Also declaring global variables is a frowned upon. Especially if they are in the global namespace. At a minimum these should be inside your own namespace, but preferably they should be inside the function.

Dyslexia strikes.
I read the next part to have std::string not std::string_view.
Prefer to pass parameters (especially those that are not mutated) by const reference. This prevents a copy.

std::vector&lt;std::string&gt; split(std::string_view const&amp; str, std::string_view const&amp; sep = "", std::size_t maxsplit = std::numeric_limits&lt;std::size_t&gt;::max())

                                               ^^^^^^^^                     ^^^^^^^^




I don't like this:

if (auto end = str.find_first_of(whitespace, start); end != npos &amp;&amp; splits &lt; maxsplit)


I did find that this is a C++17 extension. But it puts way too much information on one line. Personally I would split this across two lines.

auto end = str.find_first_of(whitespace, start);
if (end != npos &amp;&amp; splits &lt; maxsplit)


As a side note splitting the line by white space can be written more simply as:

 std::stringstream  stream(str);
 return std::vector&lt;std::string&gt;(std::istream_iterator&lt;std::string&gt;(stream),
                                 std::istream_iterator&lt;std::string&gt;());