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

138 questions
266
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
  • 18,244
  • 37
  • 110
  • 212
187
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
  • 11,247
  • 5
  • 37
  • 77
83
votes
1 answer

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
  • 6,983
  • 7
  • 40
  • 65
68
votes
2 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 programmer is not careful. On the other hand, there's no implicit conversion from…
GreenScape
  • 5,726
  • 1
  • 26
  • 51
46
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
  • 5,323
  • 3
  • 39
  • 60
38
votes
3 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,043
  • 2
  • 16
  • 27
28
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
  • 31,728
  • 22
  • 131
  • 236
23
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
  • 2,642
  • 16
  • 28
19
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
  • 365
  • 1
  • 9
19
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
  • 35,369
  • 19
  • 80
  • 136
17
votes
2 answers

Is it legal to static_cast a string_view to a string

My question is motivated by this answer on stackoverflow, https://stackoverflow.com/a/48082010/5360439. To quote, Q: How you convert a std::string_view to a const char*? A: Simply do a std::string(string_view_object).c_str() to get a guaranteed…
JohnKoch
  • 833
  • 9
  • 25
16
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
  • 1,366
  • 16
  • 30
15
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
  • 33,981
  • 13
  • 121
  • 269
15
votes
1 answer

Should methods returning const std::string& return const std::string_view instead?

Assume we have a simple getter method in a class that returns a const reference to a std::string member: const std::string& getString() const noexcept { return someString; } With the advent of std::string_view in C++17, I wonder whether it has any…
andreee
  • 3,626
  • 16
  • 33
15
votes
2 answers

Why string_view instead of generalized container_view?

I've found string_view from new C++17 standard a bit redundant. We've got a quite verbose collection of simple mechanisms for passing data to callee, without much overhead and now there is one another which is also specific only to one container…
LookAheadAtYourTypes
  • 1,451
  • 2
  • 20
  • 31
1
2 3
9 10