665 questions
7
votes
4
answers
355
views
How can I format a duration with std::println?
After running this program
(compiled with MSVC compiler 19.50.35723 with option /std:c++23preview)
#include <print>
#include <chrono>
#include <string>
int main() {
using ...
8
votes
1
answer
269
views
Why is the global namespace printf function found without importing module std.compat?
While compiling this snippet
import std;
int main() {
printf("%s", "hello");
return 0;
}
using MS VS C++ compiler 19.50 with /std:c++23preview I got the expected error ...
5
votes
1
answer
144
views
Lambda with deducing this parameter of unrelated type [duplicate]
A colleague of mine showed me a program with explicit object parameter in a lambda function. The program is accepted by two compilers, but produces a strange result:
struct A {
int a = 2;
...
14
votes
1
answer
1k
views
When and where are C++ concepts instantiated?
From https://godbolt.org/z/bnzzMn8nK:
// Example program
#include <iostream>
#include <string>
namespace bar
{
template<typename T>
concept has_foo = std::same_as<std::true_type, ...
6
votes
1
answer
199
views
Why does std::print segfault when using a string parameter?
Consider the following code:
#include <print>
int main() {
std::println("hello from {}", "me");
return 0;
}
On x86-64 this outputs hello from me. But on ARM64 it ...
Best practices
0
votes
21
replies
146
views
Explicit control of initialization & destruction of global variables
I am working in an microcontroller / embedded environment which relies on global variables. Some of these global variables have non-trivial constructors and destructors, and suffers from the static ...
2
votes
1
answer
77
views
Boost.Parser: Unexpected type for attribute
I'm currently building an expression parser in Boost.Parser. It should be able to do
Parentheses ( ( ... ) ) and abs ( | ... |)
Unary minus
Power ( x^y)
(other arithmetic operations, not relevant for ...
2
votes
0
answers
170
views
import std with GCC and CMake on Windows using a hack
I'm eager to compile programs with C++23 import std like
import std;
int main()
{
std::cout << "Hello, modules!\n";
}
using MinGW (g++15.1 or higher) with CMake (4.1 or higher).
...
13
votes
1
answer
420
views
Printing a stack trace of a hung thread
In my multithreaded program, I needed to find out from one thread the current location (stack trace) of another thread whose thread id is known.
I implemented the following solution. At startup, the ...
4
votes
1
answer
166
views
CMake looks for `std.cppm` in wrong directory when using `import std`
Recently I've decided to "modularize" my library. No success. Here is the repo where you can find CMakeLists.txt & CMakePresets.json: kissra (GitHub)
The issue is that CMake cannot find ...
13
votes
3
answers
803
views
Why does deduction guide detect template arguments as an empty set?
This code fails because the compiler thinks template arguments for Test is an empty set.
#include <tuple>
template <typename... T>
class Test
{
public:
template <typename Arg, ...
12
votes
2
answers
1k
views
How can I iterate a flat_map in a range-based 'for' loop, updating values?
It is easy to iterate std::map and update values:
#include <iostream>
#include <map>
int main() {
std::map<int, int> m;
m[1] = 2;
m[2] = 4;
for (auto &[k, v] : m)...
Best practices
0
votes
11
replies
228
views
c++ abstract class : pure virtual covariant return
first I must says that I hate templates.... it makes unreadable complex code.... not speaking about 5000000 lines long error messages, with 10000 possibilities and that takes ages to understand for ...
6
votes
1
answer
136
views
Can you observe whether _Atomic expands to an alias template or class template?
[stdatomics.h.syn] specifies the following:
template<class T>
using std-atomic = std::atomic<T>; // exposition only
#define _Atomic(T) std-atomic<T>
libc++ and libstdc++ ...
Advice
0
votes
5
replies
101
views
Can overusing the [[assume]] attribute cause problems?
I wonder if using the [[assume]] attribute can have negative effects on my code, even if the expressions inside are always true.
In my code, I defined the following macro:
#ifdef DEBUG
#include &...