Given some function void func(std::span<std::string_view>)
, how does one feed this function a raw array of C-strings const char**
in the most efficient manner?
As far as I understood this should be possible as without any copying as std::string_view
can be constructed from a C string and std::span
can be constructed from a pointer + size. However, I don't seem to be able to figure out the proper syntax.
Here's some minimal code that works when creating an std::vector<std::string_view>
by iterating over each string in the string array:
#include <iostream>
#include <string>
#include <span>
#include <vector>
static void func(std::span<std::string_view> strings)
{
// Just print the strings...
std::cout << "strings:\n";
for (const auto& str : strings)
std::cout << str << "\n";
std::cout << std::endl;
}
int main()
{
// Raw C string array
const char* raw_strings[3] = {
"This is a string",
"This is also a string",
"And then another string"
};
#if 1
// This works
std::vector<std::string_view> s;
for (std::size_t i = 0; i < 3; i++)
s.push_back( std::string_view{ raw_strings[i] } );
#else
// This does not work
std::span<std::string_view> s{ raw_strings, (std::size_t)3 };
#endif
func(s);
}
Here's a coliru link with the grunt work done: https://coliru.stacked-crooked.com/a/cb8fb8ebbc962d45