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

Why doesn't std::string have a constructor that directly takes std::string_view?

To allow std::string construction from std::string_viewthere is a template constructor template explicit basic_string(const T& t, const Allocator& alloc = Allocator()); which is enabled only if const T& is convertible to…
oliora
  • 839
  • 5
  • 12
19
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
  • 4,459
  • 22
  • 42
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…
MaxPlankton
  • 1,158
  • 14
  • 28
17
votes
1 answer

How to split a std::string into a range (v3) of std::string_views?

I need to split a std::string at all spaces. The resulting range should however transform it's element to std::string_views. I'm struggling with the "element type" of the range. I guess, the type is something like a c_str. How can I transform the…
dani
  • 3,677
  • 4
  • 26
  • 60
16
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,629
  • 2
  • 20
  • 35
15
votes
2 answers

c++17 Ambiguity when compare string_view with string

I saw both std::string_view and std::string have symmetric operator==() and for std::string it has the constructor to accept std::string_view and operator to convert itself to std::string_view. So when we try to use operator==() compare between…
golden retriever
  • 297
  • 1
  • 2
  • 10
15
votes
1 answer

string_view behaviour when passing temporary std::string

I just ran into some misunderstanding: at least in libc++ implementation std::experimental::string_view has the following concise implementation: template class basic_string_view { public: typedef _CharT…
Bikineev
  • 1,685
  • 15
  • 20
13
votes
3 answers

Correct way to printf() a std::string_view?

I am new to C++17 and to std::string_view. I learned that they are not null terminated and must be handled with care. Is this the right way to printf() one? #include #include int main() { std::string_view sv{"Hallo!"}; …
kuga
  • 1,483
  • 1
  • 17
  • 38
13
votes
1 answer

std::string_view and std::string in std::unordered_set

Let's say you have an std::unordered_set . You have an std::string_view object that you want to search for in the container. Problem is, you don't want to create a std::string from your std::string_view, as this kind of defeats the…
13
votes
2 answers

Differences between boost::string_ref and boost::string_view

Boost provides two different implementations of string_view, which will be a part of C++17: boost::string_ref in utility/string_ref.hpp boost::string_view in core/string_view.hpp Are there any significant differences between these? Which should be…
leecbaker
  • 3,611
  • 2
  • 35
  • 51
12
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,397
  • 1
  • 13
  • 33
12
votes
2 answers

When do you need a null terminated string in a read-only scenario?

I've been playing around with the std::string_view library and I have been contemplating on changing a code base I have been working on to use std::string_view as much as possible. However, in many of the threads that I have read on the subject of…
Sailanarmo
  • 1,139
  • 15
  • 41
11
votes
3 answers

How can I avoid using #define macros in C++ in a case where I have to concatenate two const char* variables?

I would like to remove the reliance of #define macros in my code, and I'm unable to achieve the same functionality with constexpr. To be practical, consider the following example: #define PRODUCT_NAME "CloysterHPC" constexpr const char* productName…
11
votes
2 answers

Implementation of string_view formatted stream ouput

While implementing C++1z's std::basic_string_view to use it on older compilers, I encountered a problem with the stream output operator overload for it. Basically, it has to output the contents referenced by the string_view while not relying on any…
nshct
  • 1,197
  • 9
  • 29
10
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
  • 590
  • 3
  • 18
1
2
3
14 15