'Convert main args to span<std::string_view> without ranges

This question has been answered before by using ranges, but I'm using C++17 with a backport of std::span - so I have spans but not ranges.

Consider this simple function:

std::span<std::string_view> cmdline_args_to_span(int argc, const char* argv[])
{
    return std::span<const char*>(argv, argc);
}

I thought the converting constructor of span would kick in due to the const char* constructor of std::string_view but it isn't. And neither is:

std::span<std::string_view> cmdline_args_to_span(int argc, const char* argv[])
{
    auto c_args = std::span<const char*>(argv, argc);
    return std::span<std::string_view>(c_args);
}

Or:

std::span<std::string_view> cmdline_args_to_span(int argc, const char* argv[])
{
    auto c_args = std::span<const char*>(argv, argc);
    return std::span<std::string_view>(c_args.begin(), c_args.end());
}

All of these fail on both Clang and gcc (using the 'real' std::span but it's the same for my backported version).

I must be doing something stupid, but I can't see what - can anyone help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source