Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly create std::string from a std::string_view?

I have a class:

class Symbol_t {
public:
   Symbol_t( const char* rawName ) {
      memcpy( m_V, rawName, 6 * sizeof( char ) );
   };

   string_view strVw() const {
      return string_view( m_V, 6 );
   };

private:
   char m_V[6];

}; // class Symbol_t

and there is a lib-func that I can't modify:

extern bool loadData( const string& strSymbol );

If there is a local variable:

Symbol_t   symbol( "123456" );

When I need to call loadData, I dare not do it like this:

loadData( string( symbol.strVw().begin(), symbol.strVw().end() ) );

I have to do like this:

string_view svwSym = symbol.strVw();
loadData( string( svw.begin(), svw.end() ) );

My question: Is the first method correct? or I must use the second one?

Because I think that in Method 1, the iterators I passed to the constructor of std::string, are of two Different string_vew objects, and theoretically the result is undefined, even though we would get expected result with almost all of the C++ compilers.

Any hints will be appreciated! thanks.

like image 266
Leon Avatar asked Dec 20 '19 11:12

Leon


People also ask

What is std::string_view for?

The std::string_view, from the C++17 standard, is a read-only non-owning reference to a char sequence. The motivation behind std::string_view is that it is quite common for functions to require a read-only reference to an std::string-like object where the exact type of the object does not matter.

Should I pass string_view by reference?

So, frequently, you can pass string_view by reference and get away with it. But you should pass string_view by value, so that the compiler doesn't have to do those heroics on your behalf. And so that your code-reviewer doesn't have to burn brain cells pondering your unidiomatic decision to pass by reference.

How do you put STD strings in C++?

In C++, you should use the string header. Write #include <string> at the top of your file. When you declare a variable, the type is string , and it's in the std namespace, so its full name is std::string .

Is std::string the same as string?

There is no functionality difference between string and std::string because they're the same type.


1 Answers

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 of sv, as if by basic_string(sv.data(), sv.size(), alloc). This overload only participates in overload resolution if std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> is true and std::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() ) );
like image 140
StoryTeller - Unslander Monica Avatar answered Sep 23 '22 06:09

StoryTeller - Unslander Monica