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
6
votes
1 answer

Why pass string_view by value? And why can't Visual Studio optimize this?

Using my gut feeling I assumed by the new string_view needed to be passed by reference, since this is more efficient (only pass pointer instead of a full class). However, several sources indicate that it's better to pass it by value, avoiding the…
6
votes
1 answer

Is there a string_view equivalent for other container types?

A string_view is: An object that can refer to a constant contiguous sequence of char-like objects with the first element of the sequence at position zero. A typical implementation holds only two members: a pointer to constant CharT and a…
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
5
votes
1 answer

When exactly are function arguments being destructed?

I have a question because it is not clear to me when function arguments get destroyed. Therefore, is the concatenation of the following doSomething function error-prone or not? I’m asking because "it is the programmer's responsibility to ensure that…
D3V0N5H1R3
  • 53
  • 4
5
votes
4 answers

How to create a constexpr array with a sequence of string_views at compile time?

I would like to create a constexpr std::array. It should for example contain constexpr std::strings_view's like this: "text0", "text1", "text2", ..... "textn" I came up with the following initial solution: #include…
Armin Montigny
  • 7,879
  • 3
  • 11
  • 29
5
votes
3 answers

Why is std::string_view faster than const char*?

Or am I measuring something else? In this code I have a stack of tags (integers). Each tag has a string representation (const char* or std::string_view). In the loop stack values are converted to the corresponding string values. Those values are…
uni
  • 397
  • 1
  • 6
  • 15
5
votes
0 answers

Is this a good practice to replace 'const std::string &' with 'std::string_view' or just 'std::string'?

I preferred const std::string & always when I need to play with std::strings. But recently, I suddenly noticed that there's no need to use const std::string & at all. The std::string_view will do better for read-only strings, and just std::string…
AcrylicShrimp
  • 150
  • 1
  • 7
5
votes
1 answer

How do gsl::string_span and std::string_view differ?

From what I can gather, gsl::string_span and std::string_view seem to have essentially the same rationale for use. Is that indeed the case? If so, are they effectively identical? If not - how do they differ? Related question: What purpose does…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
5
votes
1 answer

Is std::move safe in an arguments list when the argument is forwarded, not move constructed?

Trying to provide a solution to std::string_view and std::string in std::unordered_set, I'm playing around with replacing std::unordered_set with std::unordered_map> (the value is…
ShadowRanger
  • 108,619
  • 9
  • 124
  • 184
5
votes
1 answer

String view literals in header file

I've got a class that has a bunch of constant strings, in the form of: using namespace std::string_view_literals; class T { static const constexpr std::string_view something1 = "Alice"sv; static const constexpr std::string_view something2 =…
leecbaker
  • 3,283
  • 2
  • 31
  • 48
5
votes
1 answer

Why does passing `const char[N]` and `const char*` to view::c_str() yield different binaries, while string_view produces the same?

With std::string_view, range::for_each yields exact assembly with both const char[N] and const char * passing to std::string_view ctor In other words, this code auto str = "the quick brown fox is jumping on a lazy dog\nthe quick brown fox is jumping…
sandthorn
  • 2,364
  • 1
  • 10
  • 36
5
votes
1 answer

How to use std::string_view::remove_prefix() on a constexpr string_view

std::string_view::remove_prefix() and std::string_view::remove_suffix() are both constexpr member functions in c++17; however, they modify the variable on which they are called. If the value is constexpr, it will also be const and cannot be…
leecbaker
  • 3,283
  • 2
  • 31
  • 48
4
votes
1 answer

warning: object backing the pointer will be destroyed at the end of the full-expression for std::pair

Following code gives the dangling pointer error. std::vector> c; for (auto& b : c) { const auto& [s, i] = b; std::string_view v = s.substr(i); std::cout << v; } I think that b holds…
Jaebum
  • 1,075
  • 9
  • 26
4
votes
3 answers

How to compare string_view using if-constexpt in a constexpr context

Is it possible to compare std::string_view using "if constexpr" in a constexpr context? And why is_hello_2 and is_hello_4 fail to compile showing error: "‘s’ is not a constant expression" static constexpr bool is_hello_1(auto s) { return s ==…
Equod
  • 532
  • 3
  • 11
4
votes
1 answer

Is it safe to return a static string_view created from a string literal?

I have a relatively simple use case: I want to associate a trait to a class which will return some user defined string, namely some user-defined registration ID. As this registrations are supposed to be defined at compile-time I would like it to be…
Triskeldeian
  • 483
  • 3
  • 14
4
votes
1 answer

noexcept constructor of std::string_view

According to the documentation, std::string_view has a constructor that takes a const char * and a std::size_t, that is not declared noexcept: constexpr basic_string_view(const CharT* s, size_type count); On the other hand, the documentation states…
Giovanni Cerretani
  • 1,440
  • 1
  • 13
  • 23
1 2
3
9 10