How to correctly create std::string from a std::string_view?
There is no need to use the c'tor taking a range. std::string
has a constructor that operates in terms of std::string_view
, number 10 in the list. The effect of which is
template < class T > explicit basic_string( const T& t, const Allocator& alloc = Allocator() );
Implicitly converts t to a string view sv as if by
std::basic_string_view<CharT, Traits> sv = t;
, then initializes the string with the contents ofsv
, as if bybasic_string(sv.data(), sv.size(), alloc)
. This overload only participates in overload resolution ifstd::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>>
is true andstd::is_convertible_v<const T&, const CharT*>
is false.
Since both conditions hold for std::string_view
itself, we can write the call to loadData
as simply:
loadData( std::string( symbol.strVw() ) );