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…
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…
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…
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…
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 =…
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…
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…
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…
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.…
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…
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…
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&…
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…
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…