std::string_view oddity

  • Thread starter Thread starter HenrikCT
  • Start date Start date
H

HenrikCT

Guest
Hi,

I've been experimenting with returning std::string_view instead of std::string on accessor-methods in a very large project, but it seems that I have to change a lot of application code using the accessor-methods to do that. The problem boils down to the example code shown here:

std::string_view sv {"My test"};
std::string s1(sv); //This compile's
s1=sv; //This compile's
std::string s2=sv; //This does not compile

Why does the last line not compile? It does exactly the same as the linies above!

The compile error produced is:

c:\users\xxx\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(332): error C2440: 'initializing': cannot convert from 'std::string_view' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
c:\users\xxx\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(332): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called


Is this intentional or an error?

I know that I get a copy of the data i string_view, and that this case from a performance viewpoint is no different to returning std::string from the accessor method, but the other 10.000 compares using the same accessor-interface would benefit.

Kind regards

Henrik

Continue reading...
 
Back
Top