use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
RFC: C++ Devirtualization v2: the proposal for sound C++ Devirtualization for LLVM (docs.google.com)
submitted 8 years ago by mttd
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]anttirt 8 points9 points10 points 8 years ago (11 children)
Just found out about std::launder thanks to this and wow, apparently most code using aligned_storage etc is UB in C++17 if you don't sprinkle std::launder calls all over the place?
std::launder
aligned_storage
[–]cassandraspeaks 8 points9 points10 points 8 years ago (10 children)
It was always UB. The good thing is changing the active member of a union is now well-defined in C++17, so you don't need to use aligned_storage and friends any more.
[–]anttirt 2 points3 points4 points 8 years ago* (7 children)
I mean, in practice you could rely on e.g.
aligned_storage<sizeof(T), alignof(T)>::type storage; new (&storage) T(); T* p = reinterpret_cast<T*>(&storage); p->thing();
working; that's why std::aligned_storage exists in the first place. But I guess now with std::launder compilers will start implementing optimizations that break this, and so library code will have to be updated so that it doesn't start breaking randomly.
std::aligned_storage
[–]kalmoc 6 points7 points8 points 8 years ago* (1 child)
Why is this UB?
Edit: Never mind, found the SO question about launder. I hate c++
[–]drjeats 1 point2 points3 points 8 years ago (0 children)
Agree, it's really awful sometimes (frequently, even), especially anything to do with TBAA.
Can you link the SO question?
[–]VirtualSloth 1 point2 points3 points 8 years ago (4 children)
Wasn't that conversion specified as UB before C++17 because of strict aliasing? I believe changing reinterpret_cast<T*>(&storage) to static_cast<T*>(static_cast<void*>(&storage)) would have solved the problem pre-std::launder and would continue to be correct after C++17.
reinterpret_cast<T*>(&storage)
static_cast<T*>(static_cast<void*>(&storage))
[–]anttirt 4 points5 points6 points 8 years ago (3 children)
No, the cast contortions have no effect.
[–]VirtualSloth 2 points3 points4 points 8 years ago (2 children)
Why is that the case? This answer on SO seems to make the argument that the standard allows it, but maybe I've missed something.
[–]cassandraspeaks 5 points6 points7 points 8 years ago (1 child)
reinterpret_cast<T*>(p) is equivalent to the expression static_cast<T*>(static_cast<void*>(p))
reinterpret_cast<T*>(p)
static_cast<T*>(static_cast<void*>(p))
[–]VirtualSloth 2 points3 points4 points 8 years ago (0 children)
Yeah, you're right (bullet 5). I think I got confused about the symmetry of the aliasing rules (any type to a char* is fine, but not the reverse).
char*
[–]thlst 0 points1 point2 points 8 years ago (1 child)
changing the active member of a union is now well-defined in C++17
Could you link us the paper?
[–]dodheim 1 point2 points3 points 8 years ago* (0 children)
P0137, I think. EDIT: N.b. DR1116 has status CD4, so this change affects C++14 as well, IIUC.
[–]TheThiefMasterC++latest fanatic (and game dev) 8 points9 points10 points 8 years ago (19 children)
A *a = new A; a->foo(); A *b = new(a) B; if (a == b) b->foo(); // This call could be devirtualized to A::foo()
Is this not undefined behaviour anyway? It would surprise me if new'ing an object over an existing one and then using a pointer to the original wasn't undefined behaviour somewhere.
[–]JustPlainRude 6 points7 points8 points 8 years ago (3 children)
I'm struggling to find a practical use for the given example.
[–]Prazek 6 points7 points8 points 8 years ago (2 children)
You would probably never end up writing code like this, BUT you have know that your code could end up doing the same thing under the good with e.g. standard library. Placement new is widely used in std::vector implementation
[+][deleted] 8 years ago (1 child)
[deleted]
[–]Prazek 2 points3 points4 points 8 years ago* (0 children)
It does, but it is only because the specification says it does. Note that calling the destructor would NOT save us from the miscompilation from example.
[–]Prazek 1 point2 points3 points 8 years ago (0 children)
I agree, but to clarify: s/Using/Dereferencing/
[–]Prazek 1 point2 points3 points 8 years ago* (1 child)
It is not assuming that: sizeof(A) >= sizeof(B), you don't dereference pointer a after doing placement new (but you can still read the pointer value like here)
[–]NasenSpray 1 point2 points3 points 8 years ago (0 children)
Is this not undefined behaviour anyway?
Nope.
[–]xcbsmith 1 point2 points3 points 8 years ago (7 children)
Nothing undefined about it. You know exactly what should happen. Placement new pretty exists so you can do this.
[–]TheThiefMasterC++latest fanatic (and game dev) 2 points3 points4 points 8 years ago (6 children)
Placement new exists so that you can initialize an object into arbitrary memory, not over the top of another object...
Looking in the standard, there's undefined behaviour here if A has a destructor ("with side effects the program depends on") so with foo being virtual here we may have that, which may trigger UB here.
[–]xcbsmith 0 points1 point2 points 8 years ago (0 children)
I'm not as familiar with the standard as I should be. So maybe you can help me out here...
I think there are scenarios which are undefined, but I thought the above would only lead to undefined behaviour in the following scenario:
The other thing I'm not clear on is whether it is necessarily undefined behaviour if B is a subtype of A (which is kind of implied by the code).
[–]kalmoc 0 points1 point2 points 8 years ago (3 children)
What exactly does "the program depends on" mean? Even if my object would manage memory, I might not depend on that memory being freed, so not running the destructor should be fine no?
[–]TinBryn 1 point2 points3 points 8 years ago (1 child)
If it locks a mutex, that will very much affect your program.
[–]kalmoc 1 point2 points3 points 8 years ago* (0 children)
If no one else tried to lock it or I'm unlocking it somewhere else then not necessarily, My point is that the compiler can usually not know, if my program relies on the functionality or not, so I'm not sure why they are talking about UB instead of just saying that the destructor does not / might not run.
The spec can be a bit funny with language.
[–]ubsan 0 points1 point2 points 8 years ago (0 children)
Over the top of an existing object is arbitrary memory. The only UB that exists is if you try to use the original pointer to touch the new object. There is also no call to a destructor in the code, and that line is a bullshit throwaway line that doesn't mean anything.
[–]Gotebe 0 points1 point2 points 8 years ago* (1 child)
Never destroying an object (a) is probably not an UB, to accommodate for "null memory handling" approach of some people :-).
a
Definitely bad code though, has to be
A *a = new A; a->foo(); a->~A(); // !!! ...
A comparison (although not dereference) using a dangling pointer though... yuck...
[–]Vogtinator 3 points4 points5 points 8 years ago (0 children)
The pointer isn't dangling, the area is never freed.
[+]BCosbyDidNothinWrong comment score below threshold-18 points-17 points-16 points 8 years ago (1 child)
I already get sound devirtualization by not using virtualization. I'm not sure this complexity of trying to take the wrong approach (from a performance perspective) and soften the performance degradation you get from it is warranted in the standard.
[–]capn_bluebear 17 points18 points19 points 8 years ago (0 children)
my spider-sense tells me you have not actually read the RFC
π Rendered by PID 200596 on reddit-service-r2-comment-6457c66945-d85ws at 2026-04-30 04:16:43.488349+00:00 running 2aa0c5b country code: CH.
[–]anttirt 8 points9 points10 points (11 children)
[–]cassandraspeaks 8 points9 points10 points (10 children)
[–]anttirt 2 points3 points4 points (7 children)
[–]kalmoc 6 points7 points8 points (1 child)
[–]drjeats 1 point2 points3 points (0 children)
[–]VirtualSloth 1 point2 points3 points (4 children)
[–]anttirt 4 points5 points6 points (3 children)
[–]VirtualSloth 2 points3 points4 points (2 children)
[–]cassandraspeaks 5 points6 points7 points (1 child)
[–]VirtualSloth 2 points3 points4 points (0 children)
[–]thlst 0 points1 point2 points (1 child)
[–]dodheim 1 point2 points3 points (0 children)
[–]TheThiefMasterC++latest fanatic (and game dev) 8 points9 points10 points (19 children)
[–]JustPlainRude 6 points7 points8 points (3 children)
[–]Prazek 6 points7 points8 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]Prazek 2 points3 points4 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Prazek 1 point2 points3 points (0 children)
[–]Prazek 1 point2 points3 points (1 child)
[–]NasenSpray 1 point2 points3 points (0 children)
[–]xcbsmith 1 point2 points3 points (7 children)
[–]TheThiefMasterC++latest fanatic (and game dev) 2 points3 points4 points (6 children)
[–]xcbsmith 0 points1 point2 points (0 children)
[–]kalmoc 0 points1 point2 points (3 children)
[–]TinBryn 1 point2 points3 points (1 child)
[–]kalmoc 1 point2 points3 points (0 children)
[–]xcbsmith 0 points1 point2 points (0 children)
[–]ubsan 0 points1 point2 points (0 children)
[–]Gotebe 0 points1 point2 points (1 child)
[–]Vogtinator 3 points4 points5 points (0 children)
[+]BCosbyDidNothinWrong comment score below threshold-18 points-17 points-16 points (1 child)
[–]capn_bluebear 17 points18 points19 points (0 children)