Questions tagged [string-view]

A string_view is a C++ class template that is a non-owning reference to a string.

Reference: http://en.cppreference.com/w/cpp/experimental/basic_string_view

215 questions
365
votes
5 answers

How exactly is std::string_view faster than const std::string&?

std::string_view has made it to C++17 and it is widely recommended to use it instead of const std::string&. One of the reasons is performance. Can someone explain how exactly std::string_view is/will be faster than const std::string& when used as a…
Patryk
  • 22,602
  • 44
  • 128
  • 244
241
votes
2 answers

What is string_view?

string_view was a proposed feature within the C++ Library Fundamentals TS(N3921) added to C++17 As far as i understand it is a type that represent some kind of string "concept" that is a view of any type of container that could store something…
Drax
  • 12,682
  • 7
  • 45
  • 85
131
votes
2 answers

Why is there no support for concatenating std::string and std::string_view?

Since C++17, we have std::string_view, a light-weight view into a contiguous sequence of characters that avoids unnecessary copying of data. Instead of having a const std::string& parameter, it is now often recommended to use…
s3rvac
  • 9,301
  • 9
  • 46
  • 74
124
votes
3 answers

Why is there no implicit conversion from std::string_view to std::string?

There is an implicit conversion from std::string to std::string_view and it's not considered unsafe, even though this surely may cause a lot of dangling references if the programmer is not careful. On the other hand, there's no implicit conversion…
GreenScape
  • 7,191
  • 2
  • 34
  • 64
72
votes
1 answer

Use of string_view for map lookup

The following code fails to build on recent compilers (g++-5.3, clang++-3.7). #include #include #include void f() { using namespace std; using namespace std::experimental; map
Jean-Michaël Celerier
  • 7,412
  • 3
  • 54
  • 75
68
votes
4 answers

How you convert a std::string_view to a const char*?

Compiling with gcc-7.1 with the flag -std=c++17, the following program raises an error: #include void foo(const char* cstr) {} void bar(std::string_view str){ foo(str); } The error message is In function 'void…
Justin Raymond
  • 3,413
  • 2
  • 19
  • 28
39
votes
4 answers

When would I pass const& std::string instead of std::string_view?

I understand the motivation for using std::string_view; it can help avoid unecessary allocations in function arguments. For example: The following program will create a std::string from a string literal. This causes an undesired dynamic…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
37
votes
3 answers

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…
Leon
  • 1,489
  • 1
  • 12
  • 31
33
votes
3 answers

Safely convert std::string_view to int (like stoi or atoi)

Is there a safe standard way to convert std::string_view to int? Since C++11 std::string lets us use stoi to convert to int: std::string str = "12345"; int i1 = stoi(str); // Works, have i1 = 12345 int i2 = stoi(str.substr(1,2));…
Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
25
votes
4 answers

Using std::string_view with api that expects null-terminated string

I have a method that takes std::string_view and uses function, which takes null terminated string as parameter. For example: void stringFunc(std::experimental::string_view str) { some_c_library_func(/* Expects null terminated string */); } The…
Schtolc
  • 1,036
  • 1
  • 12
  • 19
24
votes
1 answer

Is a std::string_view literal guaranteed to be null-terminated?

I know that a trivial std::string_view is not guaranteed to be null-terminated. However, I don't know if a std::string_view literal is guaranteed to be null-terminated. For example: #include using namespace std::literals; int…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
24
votes
2 answers

How to convert std::string_view to double?

I'm writing a c++ parser for a custom option file for an application. I have a loop that reads lines in the form of option=value from a text file where value must be converted to double. In pseudocode it does the following: while(not EOF) …
patatahooligan
  • 3,111
  • 1
  • 18
  • 27
23
votes
2 answers

Why is there no overload for printing `std::byte`?

The following code does not compile in C++20 #include #include int main(){ std::byte b {65}; std::cout<<"byte: "<
Quimby
  • 17,735
  • 4
  • 35
  • 55
21
votes
1 answer

Any gotchas replacing global const char[] with constexpr string_view?

Our team is working with a 10+ years old C++ code base and recently switched to a C++17 compiler. So we are looking for ways to modernize our code. In a conference talk on YouTube I heard the suggestion, to replace const char* global strings with…
PixelSupreme
  • 395
  • 2
  • 9
20
votes
1 answer

Why does std::string_view create a dangling view in a ternary expression?

Consider a method that returns a std::string_view either from a method that returns a const std::string& or from an empty string. To my surprise, writing the method this way results in a dangling string view: const std::string&…
gexicide
  • 38,535
  • 21
  • 92
  • 152
1
2 3
14 15