244 questions
Best practices
2
votes
8
replies
189
views
What is the optimal way to define a global constant string in C++20?
I need to define several constant strings that will be used across an entire C++20 project.
I am considering the following options :
constexpr char[] str1 = "foo";
constexpr std::string str2 ...
5
votes
4
answers
349
views
How to detect use of std::string SSO (short string optimization)?
If I have a long string:
std::string data("This is a string long enough so that it is not a short string");
And then I have a view onto that string:
std::string_view dataView(std::begin(...
3
votes
2
answers
166
views
Is it safe to call free on std::string_view::data?
Is it safe to call free in the example below:
size_t len = 10;
char* buffer = static_cast<char*>(malloc(len));
std::string_view sview(buffer, len);
free(sview.data())
Why do I need this?
I have ...
0
votes
0
answers
91
views
std::cout, operator<<, u8 string literal, u8string_view : error? (glibc?) [duplicate]
I'm puzzled because it should work out of the box, and searching the web gives exactly what I done.
I need to use u8"" string literals and u8string_view to be sure to handle Unicode ...
5
votes
1
answer
203
views
Is it safe to modify std::string while there exists std::string_view pointing to it, but which is not being accessed
Consider the following C++ code:
#include <iostream>
#include <string>
#include <string_view>
void echo(std::string & input)
{
std::string_view input_v = input;
std::...
0
votes
0
answers
18
views
Printing std::source_location attributes in __cyg_profile_func_enter causes segfault?
I'm playing around with -finstrument-functions, and I'm running into things I don't understand
Let's take the following dead-simple program
// main.cc
#include <iostream>
#include <...
6
votes
1
answer
141
views
how to construct a constexpr static_string from constexpr std::string_view
I have a static_string template
template <std::size_t N>
struct static_string {
std::array<char, N + 1> elements_;
constexpr static_string() noexcept : elements_{} {}
...
1
vote
1
answer
202
views
Is it safe to pass a temporary std::string to a function accepting std::string_view?
I am working on a C++ program and have a question about the safety of passing a std::string to a function that accepts a std::string_view. Here is my code:
i make some
#include<bits/stdc++.h>
...
1
vote
0
answers
74
views
How can I create a std::string_view from integer literals/ ASCII code in a constexpr context?
GCC/ G++ supports the #embed directive also for C++ when activating GCC/ G++ extensions.
I would like to use this to populate a std::string_view constexpr from that.
#embed will create something like ...
1
vote
2
answers
144
views
How to name algorithm in c++ for process items in collection which can add or remove items? [closed]
I search for some std:: algorithm but failed. I want to process items from input iterator til last iterator and write output into out iterator. Note output can be less or more then input stream. So I ...
1
vote
0
answers
130
views
using std::string_view and unique_ptr to manage memory from malloc()
I am using the GNU readline library, which returns a char *, allocated with malloc().
The user is responsible for releasing the memory with free().
I am thinking about using std::string_view and std::...
22
votes
2
answers
1k
views
Is `std::map<std::string, int>` faster than `std::map<std::string_view, int>`?
I wanted to index substrings of a std::string whose lifetime lasts for the entire program, and relate each substring to an int using a good old std::map. The substrings are delimited by blank spaces ' ...
0
votes
0
answers
617
views
Convert string_view to char* and walk away with it
In this post, How you convert a std::string_view to a const char*?, the question was raised how to convert std::string_view into char* and the answer is basically:
make a temporary string from the ...
1
vote
1
answer
168
views
Can `split_view` or a composition of multiple `spilt_view`s be used to split a `string`/`string_view` using multiple delimiters?
I'm attempting to understand how views can be composed and the limitations of the same.
The idea here is to split a string/string_view without creating a temporary variable as shown in the second code ...
0
votes
0
answers
75
views
Why does implicit conversion fail when converting a std::basic_string to std::basic_string_view? [duplicate]
Based on this three points:
1. using string = basic_string<char, char_traits<char>, allocator<char>>
2. using string_view = basic_string_view<char, char_traits<char>>
3. ...