1,215 questions
2
votes
1
answer
155
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
176
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
164
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
150
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
132
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 ...
0
votes
2
answers
302
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
57
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
284
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
121
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
144
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 ...
22
votes
2
answers
2k
views
Why can I define a std::string instance that is constinit? Isn't constinit forbidden if an object requires dynamic initialization?
The cppreference mentions that if initialization contains a dynamic initialization part, the program is ill-formed when using constinit in C++20.
I just wonder why the following code compiles then:
...
1
vote
1
answer
149
views
How to define a generalisation to_string and to_wstring to avoid code duplication
I have a number of user-defined classes for which I would like to define a to_string function. However, instead of defining this for just std::string, I would like to define this for all the possible ...
2
votes
1
answer
106
views
String unshuffler returning different results on windows and linux
I'm working on a project that requires shuffling a string then unshuffling said string later.
Currently I shuffle the string on a Linux machine and attempt to unshuffle on a Windows machine, ...
0
votes
1
answer
116
views
Program Database Toolkit(PDT) cxxparse/cxxparse4101 error while parsering a simple c++ file contains both "#include<map>" and "std::to_string"
I came accros an error while using cxxparse/cxxparse4101 to parser a simple c++ source file. Can anyone give me a help?
c++ file:
//map.cpp
#include<iostream>
#include<map>
#include<...
1
vote
1
answer
155
views
std::string from const char* with zero allocation
When declaring std::string cpp{}; does this call new/malloc?
Assume we already have a const char* c. Is it possible to move the contents from c to cpp without extra allocations?