Skip to main content
Best practices
2 votes
8 replies
189 views

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 ...
Autechre's user avatar
  • 633
5 votes
4 answers
349 views

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(...
Loki Astari's user avatar
3 votes
2 answers
166 views

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 ...
Njrslv-yndx-cloud's user avatar
0 votes
0 answers
91 views

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 ...
delab's user avatar
  • 9
5 votes
1 answer
203 views

Consider the following C++ code: #include <iostream> #include <string> #include <string_view> void echo(std::string & input) { std::string_view input_v = input; std::...
VL-80's user avatar
  • 676
0 votes
0 answers
18 views

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 <...
bleh's user avatar
  • 1
6 votes
1 answer
141 views

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_{} {} ...
miyan's user avatar
  • 137
1 vote
1 answer
202 views

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> ...
Yan's user avatar
  • 31
1 vote
0 answers
74 views

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 ...
Torsten Knodt's user avatar
1 vote
2 answers
144 views

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 ...
leanid.chaika's user avatar
1 vote
0 answers
130 views

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::...
Tootsie's user avatar
  • 909
22 votes
2 answers
1k views

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 ' ...
Lluís Alemany-Puig's user avatar
0 votes
0 answers
617 views

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 ...
igel's user avatar
  • 556
1 vote
1 answer
168 views

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 ...
vss2sn's user avatar
  • 25
0 votes
0 answers
75 views

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. ...
PavelDev's user avatar
  • 713

15 30 50 per page
1
2 3 4 5
17