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
0
votes
0 answers

std::string_view issues on GCC8

I have some simple code using std::string_view which works great in gcc10, but not in gcc8. std::string_view was added to C++17 in GCC 7, so it should be possible to use this. #include using sv = std::string_view; sv ParseScheme(const…
Stewart
  • 4,356
  • 2
  • 27
  • 59
0
votes
1 answer

C++ - How are the arguments passed around in these lambdas?

I am taking a look at an Open Source repo with this file written in C++. The two functions use lambdas and callbacks, and I just want to better understand the order of operations and how they work together. How is this block executed when…
monk234
  • 1
  • 1
0
votes
3 answers

Can I repeat a std::string_view without copying when printing indentation?

I have a simple scene in C++ code. I want to get a long string containing some repeated std::string_view; is there a way to achieve this without copying the string? #include #include struct Logger { static inline…
Gtylcara
  • 87
  • 5
0
votes
0 answers

Mutable alternative for std::string_view?

Is there a mutable alternative to std::string_view? I need a container that stores a pointer and a size e.g. for passing it to a function instead of passing a pointer and a size. I know it's easy enough to implement it myself; I just want to know if…
SMMB
  • 107
  • 6
0
votes
0 answers

How to instantiatiate a std::basic_string_view with custom class T, I got is_trivial_v<_CharT> assert error

I would like to construct a std::basic_string_view, but the T is the customized class. Here is the test code: #include #include struct Token { Token(); Token(const Token& other) { lexeme =…
ollydbg23
  • 1,124
  • 1
  • 12
  • 38
0
votes
1 answer

C++ CRTP: how to extend the std::string_view to std::basic_string_view by passing template parameters

I'm designing a simple parser, which matches patterns. The code use some CRTP C++ design pattern, I have simplified the code as below. #include template struct parser_base { constexpr auto…
ollydbg23
  • 1,124
  • 1
  • 12
  • 38
0
votes
2 answers

What is the correct way to “clear" a std::string_view?

I see this post: c++ - Why doesn't std::string_view have assign() and clear() methods? - Stack Overflow, so string_view does not contain clear function. But in my case, I have a string_view as a class member variable, and sometimes, I would like to…
ollydbg23
  • 1,124
  • 1
  • 12
  • 38
0
votes
1 answer

Access a string view of an object inside a vector of interfaces C++

I am storing different objects which inherit from the same interface inside a single vector. Those objects have their name stored as a constant string view, which gets its value even before the object gets instantiated. I am able to easily retrieve…
Frogieder
  • 3
  • 2
0
votes
1 answer

Force decay of string literals (const char array) to ptr

In the following example I try to emplace_back() a string literal and a string_view into an std::pair of string_views. However, my problem is that emplace_back() takes up the string literal as a const char array and doesn't decay it to a pointer.…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
2 answers

How to use std::string_view with reverse iterators?

I am trying to create a very simple function: bool is_palidrome(const std::string& s) { std::string r(s.crbegin(), s.crend()); return s==r; } In order to avoid unnecessary allocations I thought I could use a string_view: bool…
apelle
  • 144
  • 1
  • 9
0
votes
1 answer

Will it be a good practice if I replace function return value from std::string to std::string_view?

I am learning c++17, I found std::string_view is a new keyword, which can improve std::string performance. It can avoid copy, according to my understanding. I have a lot of function, which return std::string, like this: std::string handle_str(const…
nothingisme
  • 119
  • 5
0
votes
1 answer

Is it possible to set default param value for the std::string_view?

Is it safe to set an empty default value for the std::string_view? void func(std::string_view arg = {} )
Hardwired
  • 804
  • 1
  • 8
  • 19
0
votes
1 answer

Join a container of `std::string_view`

How can you concisely combine a container of std::string_views? For instance, boost::algorithm::join is great, but it only works for std::string. An ideal implementation would be static std::string_view unwords(const std::vector&…
gust
  • 878
  • 9
  • 23
0
votes
0 answers

std::string_view constructor & validity outside the place it was defined

Given the following code std::string_view foo(){ std::string_view sv("ABC"); return sv; } int main(){ std::string_view svv = foo(); } Is it safe to say that since "ABC" is stored in the static area of the stack, svv is valid since the…
0
votes
2 answers

What is the name of the feature/syntax that combines curly braces, ampersand and length of character sequences?

I was reading Chapter 9 of the 2nd edition of A Tour of C++ by Bjarne Stroustrup and was puzzled by the use of {&king[0],2} where king is a string variable. I get that it returns the first 2 strings but I don't know what the name is for this to look…
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33