Skip to main content
Filter by
Sorted by
Tagged with
4 votes
0 answers
168 views

Consider this example: #include <atomic> #include <cassert> #include <thread> int main() { std::atomic<int> strong = {3}; std::atomic<int> weak = {1}; auto t1 ...
xmh0511's user avatar
  • 7,628
0 votes
0 answers
82 views

This question is a follow-up of std::bit_cast padding and undefined behavior. This "revival" is motivated by my answer to Accessing object storage where I proposed a function to modify a ...
Oersted's user avatar
  • 3,834
2 votes
0 answers
122 views

Consider this example: #include <iostream> #include <atomic> #include <thread> #include <cassert> int main(){ std::atomic<int> val = 1; std::atomic<std::atomic&...
xmh0511's user avatar
  • 7,628
2 votes
1 answer
91 views

I'm asking this question from the perspective of a compiler developer – I want to know whether a particular potential optimisation is valid (because all programs that the optimisation would break have ...
ais523's user avatar
  • 2,384
2 votes
1 answer
113 views

In the C standard (at least, I'm looking at draft C23 WG14/N3088), section 6.7.3 Type Qualifiers states: 7 If an attempt is made to modify an object defined with a const-qualified type through the ...
Evgeny Ilyin's user avatar
1 vote
1 answer
288 views

Consider the code below assuming 4 is a correct index of p->c. Are buf and *p objects alive at the same time? struct S { int x; char c; }; alignas(S) unsigned char buf[sizeof(S)]; S* p ...
Dmitriano's user avatar
  • 2,474
18 votes
1 answer
2k views

I am trying to track if a C++20 coroutine has ever suspended, so that unhandled_exception knows whether it can simply re-throw; the exception back to the caller of the initial coroutine function, or ...
LB--'s user avatar
  • 3,228
4 votes
2 answers
266 views

I am probably familiar with strict aliasing rule and implicit lifetime creation in a certain degree, but I still do not understand well enough how do these terms relate to std::launder. The following ...
Dmitriano's user avatar
  • 2,474
5 votes
1 answer
206 views

In C, if I call a static function from an inline function, I typically get a compiler warning like warning: static function <name> is used in an inline function with external linkage [-Wstatic-...
Rasmus's user avatar
  • 433
2 votes
3 answers
197 views

C23 §5.2.5.3.3 [Characteristics of floating types <float.h>] paragraph 8 says: Floating types shall be able to represent signed zeros or an unsigned zero and all normalized floating-point ...
Jan Schultke's user avatar
  • 43.7k
4 votes
1 answer
125 views

Let's say that we have a simple main.cpp file which contains only a single call of a function bar which is defined in the module a: import a; int main() { bar(1); } Then let's take a look at the ...
siga's user avatar
  • 499
2 votes
1 answer
126 views

Function templates are not allowed to have C language linkage: extern "C" { template<typename T> // error: template with C linkage void bad_f() {} } This is reasonable, as ...
yuri kilochek's user avatar
3 votes
1 answer
150 views

This is an attempt to make an alias to an anonymous class: struct { int i = 0; } a; // error C7626 in MSVC using A = decltype(a); This produces an error in Visual Studio: error C7626: unnamed ...
Fedor's user avatar
  • 24.7k
5 votes
1 answer
148 views

Both GCC and Clang reject extern "C" static void foo() {} but accept extern "C" { static void foo() {} } Aren't these supposed to be equivalent?
yuri kilochek's user avatar
2 votes
1 answer
148 views

intro.abstract p8 says: The following specify the observable behavior of the program: Accesses through volatile glvalues are evaluated strictly according to the rules of the abstract machine. Data is ...
xmh0511's user avatar
  • 7,628
8 votes
1 answer
827 views

Does the code below contain undefined behavior (UB)? struct A { int x; char y; }; int main() { std::vector<uint8_t> v(sizeof(A), 0); A* p = reinterpret_cast<A*>(v.data());...
Dmitriano's user avatar
  • 2,474
10 votes
1 answer
510 views

The order of bits in C11 bitfields is implementation-defined, and so is any padding in between them. This makes it difficult to access the bitfields by index at runtime, even though most ...
jpa's user avatar
  • 12.5k
-2 votes
2 answers
249 views

Consider this example: extern void black_box_foo(); extern void black_box_bar(); int main(){ black_box_foo(); // #1 black_box_bar(); // #2 } #1 and #2 are functions whose definitions are ...
xmh0511's user avatar
  • 7,628
4 votes
1 answer
208 views

This simple code: #include <print> int main() { volatile int i = 5; std::println("{}", i); } Doesn't compile with any major implementation of the std library. Any particular ...
ixSci's user avatar
  • 13.9k
10 votes
3 answers
1k views

I've always assumed that "creating an object" is the same thing as "starting its lifetime" (and not the same thing as allocating storage for it). But I've recently been told that &...
HolyBlackCat's user avatar
0 votes
1 answer
128 views

I'm trying to generate compile-time info about my POD types. Getting the offset of each member has been challenging due to limitations like no pointer conversions in constexpr, but I think I've found ...
Joshua Maiche's user avatar
3 votes
1 answer
151 views

The ECMAScript Language Specification states: Atomics are carved in stone: Program transformations must not cause any Shared Data Block events whose [[Order]] is seq-cst to be removed from the is-...
James Page's user avatar
1 vote
2 answers
204 views

Consider this example: #include <iostream> #include <thread> #include <atomic> int main(){ std::atomic<int> val = 0; std::atomic<bool> flag = false; auto t1 = std::...
xmh0511's user avatar
  • 7,628
16 votes
3 answers
912 views

I have a function template f, defining in its body a local class A with another nested class B. Both classes are not templates. Must I name the inner class as typename A::B or shorter variant A::B is ...
Fedor's user avatar
  • 24.7k
-2 votes
2 answers
193 views

[intro.abstract] p8 says: The following specify the observable behavior of the program: Accesses through volatile glvalues are evaluated strictly according to the rules of the abstract machine. Data ...
xmh0511's user avatar
  • 7,628
0 votes
1 answer
331 views

Consider this example: // thread A: start_transaction(); update_mysql(); commit_transaction(); // remove "key" from mysql tables remove_redis_cache("key"); // thread B: std::...
xmh0511's user avatar
  • 7,628
2 votes
0 answers
170 views

This question rises for a discussion of the answer to another question about allocation in constexpr context. cppreference states that std::allocator<T>::allocate(std::size_t n) allocates an ...
Oersted's user avatar
  • 3,834
10 votes
1 answer
300 views

Here is a possibly incorrect program for communicating between a function and a signal handler that might interrupt it. Assume that HandleSignal has been arranged to run as a signal handler. The ...
jacobsa's user avatar
  • 7,875
Advice
2 votes
4 replies
249 views

Any C++ class can be first forward declared, and defined only later in the program. Are function-local classes an exception from this rule? Please consider a simplified program: auto f() { struct ...
Fedor's user avatar
  • 24.7k
14 votes
1 answer
898 views

Rust programs typically panic when you index into a slice with an out-of-bounds usize index. Is that panic actually guaranteed to happen? Can I rely on the panic when reasoning about the soundness of (...
Aljoscha Meyer's user avatar
16 votes
2 answers
716 views

I have a program that reads the coordinates of some points from a text file using std::istringstream, and then it verifies the correctness of parsing by calling stream's operator bool(). In general it ...
Fedor's user avatar
  • 24.7k
14 votes
1 answer
373 views

Consider the following snippet: int main() { struct Local { virtual void foo() = 0; }; void (Local::* ptr)() = &Local::foo; } When compiling with C++20, GCC 13.3.0 and Clang 18.1.3 ...
OLEGSHA's user avatar
  • 728
-1 votes
0 answers
223 views
+50

Looking at this implementation of multiple-producer single-consumer, which was the implementation in Rust's standard library; however, its memory order model is derived from C++. So, it should be ...
xmh0511's user avatar
  • 7,628
5 votes
1 answer
237 views

In C, this is legal as far as I know (In C, does a pointer to a structure always point to its first member?). #include <stdio.h> typedef struct { char *name; int age; } A; typedef ...
Malyutin Egor's user avatar
2 votes
1 answer
243 views

Consider the following structure: struct foo { uint32_t a; uint16_t b; }; Assume that this structure has 2 padding bytes at the end (a likely case) and suppose that an instance is set up like ...
nielsen's user avatar
  • 8,091
0 votes
1 answer
164 views

[intro.execution] p8 says: Given any two evaluations A and B, if A is sequenced before B (or, equivalently, B is sequenced after A), then the execution of A shall precede the execution of B. ...
xmh0511's user avatar
  • 7,628
2 votes
2 answers
194 views

Im wondering if there is a better, more typesafe, way to return "a selection" than I do at the moment. If we only ever have one selected element at a time, we can just return a reference, ...
darune's user avatar
  • 11.5k
0 votes
2 answers
159 views

In order to examine object representation, the C++ standard provides https://timsong-cpp.github.io/cppwp/n4861/basic.lval#11: If a program attempts to access ([defns.access]) the stored value of an ...
Oersted's user avatar
  • 3,834
3 votes
1 answer
225 views

I'm working on legacy code that uses mmap to load data from a file: int fd; std::size_t fs; fd = open(filename, O_RDONLY); // error management omitted for the example fs = get_size_from_fd(fd); // ...
Oersted's user avatar
  • 3,834
0 votes
1 answer
178 views

Consider this example: #include <atomic> #include <iostream> #include <chrono> #include <thread> #include <cassert> int main(){ std::atomic<int> val = {0}; ...
xmh0511's user avatar
  • 7,628
21 votes
1 answer
655 views

C++ allows us to make union class type without any data members. (cppreference even contain an example with it.) Can one request the compiler to provide default implementation of equality comparison ...
Fedor's user avatar
  • 24.7k
10 votes
1 answer
290 views

This is a multi-producer single-consumer implementation translated from Rust, for the language-lawyer question, rewriting it in C++ template<class T> struct Node{ std::atomic<Node*> ...
xmh0511's user avatar
  • 7,628
1 vote
2 answers
310 views

So there is a phrase from the C++ Standard working draft (n4928): [dcl.init.general] ¶ 16.6.1 If the initializer expression is a prvalue and the cv-unqualified version of the source type is the same ...
Eugene Zavidovsky's user avatar
3 votes
1 answer
246 views

Can someone help me understand this part of cppreference? https://en.cppreference.com/w/cpp/language/default_comparisons.html Synthesized three-way comparison The synthesized three-way comparison of ...
a a's user avatar
  • 384
5 votes
1 answer
154 views

To avoid recursive template programming I am relying on the short circuit evaluation of the result of lambdas combined with logical && using a fold expression. Is that safe to do or will the ...
phinz's user avatar
  • 1,647
7 votes
1 answer
334 views

C11 introduced <stdalign.h>, which defined the macros alignas to _Alignas and alignof to _Alignof. Additionally, the feature test macros __alignas_is_defined and __alignof_is_defined are defined ...
DevSolar's user avatar
  • 71k
7 votes
1 answer
202 views

In C++, forward declarations introduce an incomplete type. Incomplete types can be used to declare pointers, but they cannot be used to initialize values or access members because the complete ...
Connor Lawson's user avatar
0 votes
0 answers
117 views

Based on cppreference, I was under impression that std::canonical and std::weakly_canonical return the input path if it is an absolute directory path to existing directory that has no dot, dot-dot ...
Fedor's user avatar
  • 24.7k
0 votes
1 answer
180 views

Consider this example: #include <atomic> #include <thread> int main(){ std::atomic<int> v0 = 0; std::atomic<int> v = 0; std::thread t1([&](){ v0.store(...
xmh0511's user avatar
  • 7,628
15 votes
1 answer
1k views

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 ...
Dominik Kaszewski's user avatar

1
2 3 4 5
180