I am working on the following code:
std::string myFunc(std::string_view stringView)
{
size_t i = stringView.find_last_of("/\\");
if (i != std::string::npos)
return stringView.substr(i + 1);
return stringView;
}
int main()
{
std::string testString{"TestString"};
std::string_view testView{testString};
std::string test{testView};
return 0;
}
where I am passing a string_view to the function myFunc(). The function returns a std::string since I've read that it's better to not return a string_view.
However, the compiler is giving the following two errors:
<source>: In function 'std::string myFunc(std::string_view)':
<source>:19:31: error: could not convert 'stringView.std::basic_string_view<char>::substr((i + 1), ((std::basic_string_view<char>::size_type)std::basic_string_view<char>::npos))' from 'std::basic_string_view<char>' to 'std::string' {aka 'std::__cxx11::basic_string<char>'}
19 | return stringView.substr(i + 1);
| ~~~~~~~~~~~~~~~~~^~~~~~~
| |
| std::basic_string_view<char>
<source>:20:12: error: could not convert 'stringView' from 'std::string_view' {aka 'std::basic_string_view<char>'} to 'std::string' {aka 'std::__cxx11::basic_string<char>'}
20 | return stringView;
| ^~~~~~~~~~
| |
| std::string_view {aka std::basic_string_view<char>}
What is the correct way to solve this if I want a std::string as returned type? See the link for the compiler error snapshot
Thanks in advance
Creating a local string and initialize it with the string_view could be a solution (?)
std::stringdoesn't have a constructor that takes astd::string_view. That seems like an oversight, but not much you can do about it.string_view"std::string_viewhas no ownership, so you have to be careful about lifetime when returningstd::string_view, it seems correct for your code to returnstd::string_view.