1,219 questions
Advice
1
vote
5
replies
162
views
C++ convert vector element to string
Why can't you just do this?
int index = 0;
std::string example;
std::cin >> example;
std::vector<std::string> list;
list.push_back(example[index]);
I'm a complete beginner in c++ and I ...
8
votes
1
answer
571
views
Why does gcc 8 cause double free in this std::string example
The following test code causes a double free error on gcc 8.5 or lower when run with g++ test.cpp -O2 -std=c++1z -fnon-call-exceptions. On other gcc versions like 9.1 or higher it doesn't.
See ...
-2
votes
1
answer
184
views
Implementation of std::string class members [duplicate]
class string {
struct long_mode {
size_t size;
size_t capacity;
char* buffer;
};
struct short_mode {
uint8_t size;
char buffer[23];
};
...
Advice
0
votes
30
replies
159
views
Can all std::basic_string constructors throw std::length_error?
cppreference.com says:
std::basic_string<CharT,Traits,Allocator>::basic_string
Throws std::length_error if the length of the constructed string would exceed max_size() (for example, if count &...
0
votes
1
answer
109
views
std::string - constexpr fails to compile if string length is over the capacity [duplicate]
I have this demo. It fails to compile if the provided initialisation string for constexpr std::string OVERFLOW = is longer than the default capacity of 15. I understand why, but I am not aware of ...
2
votes
1
answer
167
views
How to assign a std::string from std::put_time in C++?
I need to set the result from std::put_time from ctime into a std::string, how do I do this ?
Here is my code:
BBToolkit::LogManager::LogManager() {
auto t = std::time(nullptr);
auto tm = *std:...
5
votes
0
answers
182
views
Short string processing in const expressions of Visual C++
I observe rather strange behavior of Visual Studio's compiler with short strings during constant evaluation, as can be demonstrated by this example:
#include <string>
constexpr char f( const ...
2
votes
1
answer
173
views
Appending formatted content to a `std::string` without creating temporaries
I want to create the following string: "name_1,name_2,...,name_100".
This can be accomplished using the following code:
#include <format>
#include <string>
// ...
const auto ...
3
votes
0
answers
155
views
C++ std::ssize works in one file but not another
I seem to run into issue with the use of std::ssize in C++.
I am able to compile in string.cpp but not string_quiz.cpp.
I ran this code with g++ -o string_quiz -std=c++23 string_quiz.cpp
string_quiz....
3
votes
1
answer
137
views
Do objects part of a class not get constructed until its constructor is called
For this simple classes below. For MyClass, the constructor initializes str with s during construction using the intializer list.
Does this mean that str doesn't get constructed at all yet until mc ...
1
vote
2
answers
331
views
How to resize a std::string and have it have the same capacity?
If I have an empty string and I do .resize() I expected the size and the capacity to be that size. That is not the case in GCC or Clang or MSVC. Requesting a size of 17 gives you a capacity of 30 or ...
1
vote
0
answers
59
views
std::string constructor not creating string from iterators [duplicate]
I'm trying to convert an std::array of integers to a string for output, so I tried using this string constructor:
template< class InputIt > basic_string( InputIt first, InputIt last, const ...
9
votes
0
answers
290
views
std::string comparison not constexpr in clang?
I want a templated function that calls a method of its template class depending on which class it is. I understand that a better way to achieve this would be using std::is_same, but irrelevant for the ...
2
votes
2
answers
125
views
snprintf to pre-sized std::string doesn't work? [duplicate]
I'm a bit lost why Option 1 doesn't work. In my understanding I should be able to write to a presized std::string whatever I want as long as I don't overshoot the string size, but it doesn't work on ...
1
vote
1
answer
151
views
Optimise C++ const std::string literal declaration for compile time initialization and runtime performance
I wish to optimise the use of const string literals in a bulky c++ 20 project to enhance compilation and runtime performance
In lots of source files string literals are declared with const std::string ...