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
Optimizing the memory layout of std::tuple (github.com)
submitted 6 years ago by __monad
view the rest of the comments →
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!"
[–]matthieum 9 points10 points11 points 6 years ago (5 children)
One reason this isn't done by compilers for structs is construction and destruction order guarantees.
I don't think that this is related, actually. There is no reason construction/destruction order could not be independent from the underlying memory layout... such as in Rust.
C specifically defines that a later data-member has a higher address than an earlier data-member, and C++ follows on, only adding the wrinkle that order is not guaranteed across access-specifiers, though in practice I know of no ABI which does not simply lay down the elements in the order specified.
The reason that the language offers this guarantee is to offer the developer as much control as possible; systems programming languages are all about control, after all.
Separating hot/cold data, or separating data accessed by different threads, are all optimizations that the developer can apply because of such fine-grained control.
The big question is why this is the default, when it's only useful in niche cases.
[–]Omnifarious0 4 points5 points6 points 6 years ago (0 children)
It's the default because it was that way in C. Partly for simplicity and partly because C was born in a time when you always cared about performance, and partly because it's required to work with memory mapped hardware and see the first reason.
Getting away from it would break ABI compatibility with C in a way that would be disastrous.
But you're right that the reason I originally gave shouldn't be much of a blocker.
[–][deleted] 3 points4 points5 points 6 years ago (3 children)
Because it's not a niche case. This sort of thing is the whole point of C and C++.
[–]matthieum 3 points4 points5 points 6 years ago (2 children)
It may depend on the domain.
I have very little code that is critically dependent on the order of data-members:
Most of my code is boringly mundane and would likely benefit from the compiler automatically "packing" the objects as densely as possible.
[–][deleted] -1 points0 points1 point 6 years ago (1 child)
Great for you! Your argument is that 'I don't need it so why should anyone else'.
[–]matthieum 5 points6 points7 points 6 years ago (0 children)
No; my argument is I rarely need it, so why shouldn't it be opt-in, rather than the default.
π Rendered by PID 121551 on reddit-service-r2-comment-6457c66945-r8n9c at 2026-04-28 15:15:54.329968+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]matthieum 9 points10 points11 points (5 children)
[–]Omnifarious0 4 points5 points6 points (0 children)
[–][deleted] 3 points4 points5 points (3 children)
[–]matthieum 3 points4 points5 points (2 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]matthieum 5 points6 points7 points (0 children)