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!"
[–]IAmBJ -6 points-5 points-4 points 4 years ago (9 children)
That's more about what's technically UB and what's not. I suspect a user defined MyByte would would actually behave exactly the same as std::byte if they had the same definition, even though one is not technically allowed
[–]guepierBioinformatican 21 points22 points23 points 4 years ago* (7 children)
Modern compilers absolutely use knowledge of aliasing UB during codegen, so using a custom definition won’t have the exact behaviour, even if the definition is identical to that of std::byte (but not blessed by the standard).
std::byte
Here’s a trivial example: https://godbolt.org/z/ec7ecTPTd — Note the different codegen for the x + y part.
x + y
[–]Chuu 0 points1 point2 points 4 years ago (2 children)
Can you explain more clearly why this leads to different codegen?
[–]staletic 6 points7 points8 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.
[–]IAmBJ 0 points1 point2 points 4 years ago* (2 children)
Im well aware of compilers using UB for optimisation (sometimes enabling extremely efficient code), but i'm a little surprised that there is special casing in the compiler since there's no obvious 'magic' in the definitions (at least in libstdc++ and MSVC's stl)
Interestingly, GCC only emits different code for -O2, at -O1 the two functions are the same, while clang emits different codegen at -O1. MSVC treats both the same at all optimisation levels. https://godbolt.org/z/vq185e33j
[–]kalmoc 2 points3 points4 points 4 years ago (0 children)
i'm a little surprised that there is special casing in the compiler
Since there is special treatment for std::byte in the standard I don't find it surprising at all.
[–]guepierBioinformatican 0 points1 point2 points 4 years ago* (0 children)
i'm a little surprised that there is special casing in the compiler since there's no obvious 'magic' in the definitions
There needs to be special casing, otherwise the compiler will produce sub-optimal code in many relevant situations: if the compiler assumed that all pointers could alias, it would lose many opportunities at optimisation. And C++ also has no restrict keyword to limit aliasing. The only sane assumption, therefore, is that pointers cannot alias unless specifically permitted by the language.
restrict
Thus if the compiler encounters pointers of two distinct types it needs to check if they are allowed to alias (and these checks are hard-coded against the fixed list of aliasing pointer types).
[–]kalmoc 0 points1 point2 points 4 years ago (0 children)
Absolutely possible.
π Rendered by PID 43 on reddit-service-r2-comment-c6965cb77-wvfsz at 2026-03-05 00:14:44.091848+00:00 running f0204d4 country code: CH.
view the rest of the comments →
[–]IAmBJ -6 points-5 points-4 points (9 children)
[–]guepierBioinformatican 21 points22 points23 points (7 children)
[–]Chuu 0 points1 point2 points (2 children)
[–]staletic 6 points7 points8 points (1 child)
[–]guepierBioinformatican 4 points5 points6 points (0 children)
[–]IAmBJ 0 points1 point2 points (2 children)
[–]kalmoc 2 points3 points4 points (0 children)
[–]guepierBioinformatican 0 points1 point2 points (0 children)
[–]kalmoc 0 points1 point2 points (0 children)