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