6,212 questions
Advice
2
votes
2
replies
58
views
Why do people use macros to declare namespaces in c++?
Off the top of my head, I only remember LLVM's standard library do this, but I remember seeing other libraries also do this. I'm not entirely sure what is the advantage here. I feel like namespace std ...
Advice
0
votes
6
replies
196
views
C++ compile type endianness for use in macros
I need to layout a structure in two different ways depending on the endianness of the target platform. Currently I'm using an additional pre-compile phase to run a program to test the endianness, and ...
-1
votes
1
answer
225
views
What are the disadvantages of std::try_lock()?
I am currently studying the work of the mutex and I have a question, if we have the std::try_lock() method, then why not always use it instead of std::lock(), what are its disadvantages?
For example, ...
Advice
1
vote
7
replies
179
views
How does Microsofts std::lib implementation perform debug iterator checks?
In the question Why does Microsoft's implementation of std::string require 40 bytes on the stack? the observation is made, that a std::string requires 8 additional bytes in Debug mode.
After ...
0
votes
0
answers
79
views
Debug configuration build with OR'ing std::wregex constansts throws an unexpected std::regex_error exception
I'm compiling this with VS 2022 C++ compiler, as "ISO C++20 Standard (/std:c++20)" for a Debug configuration.
How come this throws the following std::regex_error:
regex_error(error_backref):...
6
votes
4
answers
283
views
Constexpr function that builds an integral value from a list of bits
Sometimes, it may be useful to build an integral value from a list of bits (in increasing order). Such a function could be named to_integral.
Example:
static_assert (to_integral(1,1,0,1,0,1) == ...
0
votes
0
answers
114
views
Does std::is_invocable_r with void as return type lead to UB?
Today I stumbled upon weird behavior of std::is_invocable_r.
While doing research for why it is happening, I stumbled upon this question on Stack Overflow:
is_invocable_r ignoring the return parameter
...
15
votes
1
answer
1k
views
Is it Undefined Behavior to backport namespace std features to older C++ versions?
According to What are the reasons that extending the std namespace is considered undefined behavior?, adding anything to namespace std is Undefined Behavior, with some exceptions carved out, such as ...
0
votes
1
answer
374
views
Apple Clang 21 unable to compile with c++ 23 stacktrace header
I am trying to use the C++ 23 stacktrace header in my project but I can't get it to compile. I've reproduced this with a minimal case to demonstrate the error I am getting. Any input would be greatly ...
5
votes
0
answers
202
views
Why is std::atomic<T> larger than T itself for user-defined structs on MSVC but not on GCC/Clang?
I was checking the size of std::atomic compared to T on different platforms (Windows/MSVC, Linux/GCC, Android/Clang).
For intrinsic types (like int, int64_t, etc.), the size of std::atomic matches the ...
0
votes
1
answer
137
views
Why template instantiation requires specialation in the case where it is already done for all cases?
In c++ when i try to write a code that returns reverse of a type list what i wrote( a user defined template class):
template <typename ...V,typename ...D>
constexpr decltype(auto) merge(...
5
votes
1
answer
143
views
Use std::regex_search called from module
I came across a strange problem. I made a C++20 module with the following content (file module.cppm):
module;
#include <regex>
#include <string>
export module foo;
export namespace bar {
...
1
vote
0
answers
166
views
Weird interaction between libstdc++ standard library headers and module
I'm using clang 21.1.0 from linuxbrew's LLVM package with libstdc++ from Fedora 42's repos (gcc 15.2.1). The following code compiles:
#include <memory>
#include <string>
auto main() -> ...
4
votes
2
answers
431
views
Including c++ standard headers from C not allowed, what should I use instead?
I'm trying to modernize an old code base that's written in a combination of C and C++.
My first task is to get it compiling with cl.exe v19.44.35214.
I'm getting a lot of this error in yvals_core.h:
#...
4
votes
2
answers
220
views
Can the back() iterator of a vector be safely assumed to be the end() iterator after a pop_back()?
My problem is the following :
std::vector<struct pollfd> vec = { ... }; // Actually a member variable on a Server object
for (auto iter = vec.begin(); iter != vec.end(); ) {
if (...
2
votes
1
answer
140
views
C++ Enum storage size conversion prevention
Is is possible to prevent explicit enum storage size conversions? I tried overloading = but this does not seem to work on primitive types.
enum GlobalSettingTableType : std::uint16_t
{
first = 0,
...
0
votes
0
answers
90
views
C++ wofstream/wifstream: problems with numeric values in binary format
I'm trying to use std::wofstream/wifstream to serialize/deserialized mixed numerical (e.g., int, double) and std::wstring content, with the numerical values serialized in binary mode. But the ...
2
votes
1
answer
220
views
std::unique() algorithm returns clearly non-unique results [duplicate]
I have been experimenting with C++'s std::unique() algorithm, but the results it returns really confuse me.
I have done a simple function to test it, like so:
#include <algorithm>
#include <...
4
votes
1
answer
295
views
Pros and cons of make_unique vs direct constructor call in C++17 [closed]
The function std::make_shared<T>() is most often preferred to std::shared_ptr<T> {new T}, because it will only make one allocation, as opposed to two allocations in the second example. ...
2
votes
1
answer
144
views
Using std::move with Constructor(Type t) or Constructor(Type&& t). What's the difference?
I'm studying std::move semantics and I would like this to be clarified to me:
Say I have:
// Message.h
class Message
{
private:
std::array<uint8_t, BUFFER_SIZE> buffer;
std::queue<...
1
vote
2
answers
218
views
C++ map whose key is a string within the value
Say I have a C++ class containing an id:
class MyData {
std::string _id;
std::vector<int> _stuff;
public:
const std::string& id() { return _id; }
MyData(std::string id) : ...
10
votes
1
answer
174
views
How to correctly determine the seed type for the various random number engines in the c++ standard library
Recently there was a discussion on the reddit c++ subedit pertaining to the difficulties of using the random library. One of the issues was how to correctly seed a random number engines.
std::...
0
votes
1
answer
192
views
std::cout not working. Code runs, but just prints absolutely nothing [closed]
Here is the extremely simple code in C++:
#include <iostream>
int main() {
std::cout << "hello world" << std::endl;
return 0;
}
When I purposefully create an ...
4
votes
0
answers
189
views
Questions regarding the new C++ fixed-width floating-point types in C++23
With the introduction of the new fixed-width floating point types in C++23 (the std::floatN_t types in <stdfloat>), some guarantees regarding the representation of floating-point types are not ...
3
votes
0
answers
67
views
Named pipes/FIFO stalling on macOS with C++
The code below works as expected on Linux when run as follows:
mkfifo /tmp/{a,b}
./a.out 0 & ./a.out 1 &
results in
hello
bye
hello
bye
...
On macOS, however, it just stalls. It seems that ...
3
votes
0
answers
73
views
Can I choose where Visual Studio builds "ISO C++23 Standard Library Modules"?
I have Visual Studio 2022 v17.14.1 compile ISO C++23 Standard Library Modules according to the setting below in several projects:
Unfortunately, each project seems to compile their own copy of the ...
2
votes
1
answer
706
views
cmake ignores CMAKE_EXPERIMENTAL_CXX_IMPORT_STD flag regardless of when it is called
I have a very simple script:
cmake_minimum_required(VERSION 4.0 FATAL_ERROR)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444")
set(CMAKE_CXX_MODULE_STD 1)
project(...
-2
votes
1
answer
310
views
can't find libc++ packages on el9
I have a c++ application that I automatically build and release for various platforms. I use docker images to build for Linux distros.
Recently I ran into a libstdc++ bug in std::regex (very old, ...
3
votes
2
answers
99
views
Cannot detect protected base class method with SFINAE
I attempted the following approach using c++ SFINAE and std::declval to find if there exists the proper method in a base class, that can be called from the derived class. The derived class gets the ...
3
votes
3
answers
272
views
Why does std::filesystem::absolute resolve . and .. on Windows but not on POSIX platforms?
I'm using C++17's std::filesystem::absolute to convert relative paths to absolute ones. I noticed that on Windows (MSVC), this function seems to resolve . and .. components in the path, but on Linux (...
5
votes
1
answer
164
views
How to parallelize a sum over an arbitrary column of a vector of vectors using C++ standard library execution policies? [closed]
I'm trying to parallelize a part of a larger program using the C++ standard library and its execution policies. The original program uses std::accumulate to calculate sums over columns of 2d vectors (...
26
votes
3
answers
2k
views
Only copiable type not accepted in msvc std::vector implementation
In the following code:
struct copy_only
{
copy_only() = default;
copy_only(const copy_only&) = default;
copy_only& operator=(const copy_only&) = default;
...
0
votes
1
answer
126
views
Difference between reading large chunks of data using char* or std::string
I was trying to solve a coding problem and, to summarize, I was using this piece of code:
char result[50005][26], buffer[50005];
while (fin >> buffer) {
for (int i = 0; i < strlen(...
1
vote
1
answer
153
views
Can `std::unordered_map::iterator` be implemented without reference to underlying map?
A while back I ported some of the C++ stdlib containers to environment where stdlib was not available. While iterators for contiguous and node-based containers were easy, I was stumped how to ...
2
votes
1
answer
235
views
Can I get clang-tidy to warn about the use of indirectly-included standard library constructs
Consider the following program:
#include <array>
int main() {
throw std::logic_error("hi");
}
With some C++ compilers (or rather, standard libraries) - this compiles. For example,...
0
votes
0
answers
74
views
Unable to have all pieces be seen on grid format using VertexArray with SFML and C++
I am facing difficulties with my C++/SFML project that is focused on replicating the game of Blokus. I've been trying to get all the pieces to be seen in initial loading and am also unable to have the ...
2
votes
0
answers
160
views
C++ ranges: Change a parameter within a view
given the following MWE
#include <iostream>
#include <vector>
#include <ranges>
#include <algorithm>
#include <numeric>
namespace{
auto doSomething(const bool b)
{
...
3
votes
2
answers
187
views
C++ portable IPC
I am porting a console application that was written in C targeting Linux. I want to make the code as portable as possible, so that it can also be used on Windows and other OSs. So far, I was able to ...
2
votes
1
answer
127
views
Convert a std::variant<std::monostate, T...> to std::variant<T...>
I have an object of type std::variant<std::monostate, T...>, but I need to call a function that only accepts std::variant<T...>.
Is there a way I could convert a std::variant<std::...
3
votes
2
answers
267
views
Why does std::queue not use forwarding reference for push?
Why is push for std::queue<T, Container> defined as
void push( const value_type& value );
void push( value_type&& value );
instead of a single member function with forwarding ...
7
votes
1
answer
215
views
How to determine if a Type supports printing or formatting using C++ standard lib?
I would like to know if there is built-in checkers in std which checks if a type is printable via operator<< or if it is formattable via std::format.
I tried to search it and could not find ...
2
votes
1
answer
93
views
std::get_time doesn't always give the same result
What I want to do
I am working on a small discord bot to handle friendly bets on ESport matches with my friends. Lately I have been trying to add the date and hours of the matches to:
Display only ...
0
votes
2
answers
224
views
Two threads alternating (ping-pong) execution
Is it possible to make guaranteed alternating execution of two threads without using two atomics (or other stuff like semaphores, etc.)? I want to make sure the both threads execute, preferably if one ...
3
votes
0
answers
134
views
How to store std::shared_ptr in std::vector in thread-safe way? [closed]
I have many threads which share access to the same pool of objects, and some threads could remove objects, so I use std::vector<std::shared_ptr<T>> to hold pointers to the objects so that ...
6
votes
0
answers
206
views
Why std::find_if significantly accelerates function only on Intel CPUs?
Rewriting the following function using std::find_if yields roughly 2x speed up, but only when given Intel CPU.
bool process_update_range_based(int treeId, int newHeight, bool tall,
...
10
votes
5
answers
683
views
How can I transparently process std::vector of T and std::vector of std::shared_ptr<T> in a template?
I want to apply the same template algorithm to std::vectors which contain objects of some type T and (different) std::vectors which contain std::shared_ptrs to objects of some type T.
Can I ...
2
votes
1
answer
137
views
How does std::sort check if an iterator is std::random_access_iterator_tag?
I read that std::sort only accepts random access iterators. So, I expected to find code in the sort function that ensures only random access iterators are accepted, but I couldn't find any.
Why is it ...
2
votes
0
answers
132
views
How should I declare an interface function that gets a range of ints?
I want to pass a std::ranges::view to a function which is an implementation of an interface, without creating a new vector and allocating more memory.
virtual void doSomethingWithInts(std::ranges::...
4
votes
1
answer
110
views
How do I control the type of value returned by std::ranges::iota_view?
C++ 20 introduces std::ranges::iota_view.
It may be used as part of a for loop, for example.
for (auto i: std::ranges::iota_view(0, 10))
It appears to take two template parameters. For example, the ...
1
vote
1
answer
105
views
std::priority_queue pre-allocate memory memory error
I'm working on a deterministic real time project which has an execution cycle per iteration of couple of hundred micro-seconds. I'm facing issue where by the push operation on a std::priority_queue ...