I'm not able to provide a std::string_view
to std::istringstream
's constructor. The following code doesn't compile (C++17 enabled using Clang v8):
std::string_view val = "Hello";
std::istringstream ss(val, std::ios_base::in);
The error I get is:
prog.cc:9:24: error: no matching constructor for initialization of 'std::istringstream' (aka 'basic_istringstream<char>')
std::istringstream ss(val, std::ios_base::in);
^ ~~~~~~~~~~~~~~~~~~~~~~
/opt/wandbox/clang-6.0.0/include/c++/v1/sstream:651:14: note: candidate constructor not viable: no known conversion from 'std::string_view' (aka 'basic_string_view<char>') to 'const std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >::string_type' (aka 'const basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') for 1st argument
explicit basic_istringstream(const string_type& __s,
^
However this does:
std::string_view val = "Hello";
std::istringstream ss(val.data(), std::ios_base::in);
This issue is weird to me because there's only 1 implicit conversion that should be happening here: std::string_view
to std::basic_string
. The constructor is taking a basic_string
according to the error message.
Why can't I use string_view
verbatim here without calling string_view::data()
?
string_view::data()
is not even right, as the stringstream will be expecting a null-terminated string. – Rhodenstring_view
just makes things more complicated & confusing than before, where things were eitherstd::string
orchar*
. So far I'm not enjoying it. – Stipplestring_view
, looking for a null char. – Apus