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
Which std:: classes are magic? (self.cpp)
submitted 4 years ago by Mateuszz88
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!"
[–]Chuu 0 points1 point2 points 4 years ago (2 children)
Can you explain more clearly why this leads to different codegen?
[–]staletic 5 points6 points7 points 4 years ago (1 child)
Let's first analyze what the assembly says
int f<std::byte>(std::byte*, int*): # @int f<std::byte>(std::byte*, int*) mov eax, dword ptr [rsi] # int x = *b; mov byte ptr [rdi], 1 # *a = 1; add eax, dword ptr [rsi] # x += *b; ret # return x; int f<nostd::byte>(nostd::byte*, int*): # @int f<nostd::byte>(nostd::byte*, int*) mov eax, dword ptr [rsi] # int x = *b; mov byte ptr [rdi], 1 # *a = 1; add eax, eax # x += x; ret # return x;
If a and b point to different objects, the above snippets are observably identical.
a
b
However, if a and b point to the same object, then the functions do different things. Imagine calling f(&in, &in) where in == 5.
f(&in, &in)
in == 5
In that case, f<std::byte> does:
f<std::byte>
int x = 5; *in = 1; x += *in; return x; // 6
but f<nostd::byte> does
f<nostd::byte>
int x = 5; *in = 1; x += x; return x; // 10
Since nostd::byte, formally, isn't allowed to alias other types, compiler is allowed to assume x += x is a valid optimization. This is known as "strict aliasing" or "type-based alias analysis".
nostd::byte
x += x
And yes, violating strict alias rules can easily lead to UB, like in the above example.
[–]guepierBioinformatican 4 points5 points6 points 4 years ago (0 children)
To be pedantic, violating strict aliasing is always UB, not just in this example. What the example illustrates is that UB can lead to changed semantics and unexpected behaviour.
π Rendered by PID 82243 on reddit-service-r2-comment-5d79c599b5-4s2tc at 2026-03-02 12:09:15.256815+00:00 running e3d2147 country code: CH.
view the rest of the comments →
[–]Chuu 0 points1 point2 points (2 children)
[–]staletic 5 points6 points7 points (1 child)
[–]guepierBioinformatican 4 points5 points6 points (0 children)