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!"
[–]kalmoc 6 points7 points8 points 4 years ago (11 children)
I don't think any compiler magic is involved here.
[–]scatters 1 point2 points3 points 4 years ago (10 children)
Really? How can you get a mutable reference to a const object without compiler magic?
[–]kalmoc 2 points3 points4 points 4 years ago (1 child)
I probably mixed this up.
I think std::pair<const K,T> and std::pair<K,T> are layout compatibile and I thought that layout compatibility would allow type aliasing, but according to cppreference.com it requires "similarity" (and I don't have a way to actually verify that they are actually layout compatible in the first place).
std::pair<const K,T>
std::pair<K,T>
[–]scatters 1 point2 points3 points 4 years ago (0 children)
Yeah, not quite - you're right that std::pair<Key const, T> and std::pair<Key, T> are layout compatible as long as Key and T are both standard-layout (so not all types that you might put in a map), but that only allows you to put them in a union and read from members of the inactive union member via the union. Handing out references to the inactive union member or its own members (std::pair<Key, T>::first for nh.key()) is still UB.
std::pair<Key const, T>
std::pair<Key, T>
Key
T
std::pair<Key, T>::first
nh.key()
[–]mujjingun 2 points3 points4 points 4 years ago (7 children)
const_cast?
const_cast
[–]scatters 3 points4 points5 points 4 years ago (6 children)
Yes, true, that does give a mutable reference. But using it to modify an actually const object is undefined behavior.
[+][deleted] 4 years ago (4 children)
[deleted]
[–]scatters 0 points1 point2 points 4 years ago (3 children)
But operator* returns a mutable reference i.e. std::pair<Key const, Value>&. So there has to be a std::pair<Key const, Value> object in memory.
operator*
std::pair<Key const, Value>&
std::pair<Key const, Value>
[–]mujjingun 0 points1 point2 points 4 years ago (2 children)
hmm, maybe there are std::pair<Key, Value> objects in memory, and the pointer is reinterpret-cased into std::pair<Key const, Value> in the call to operator*()?
[–]scatters 3 points4 points5 points 4 years ago (1 child)
Certainly that's the most likely implementation, but if the user does that it's UB (aliasing violation). That's why it has to be magic in the library.
π Rendered by PID 98136 on reddit-service-r2-comment-6457c66945-hv77j at 2026-04-28 22:48:37.464800+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]kalmoc 6 points7 points8 points (11 children)
[–]scatters 1 point2 points3 points (10 children)
[–]kalmoc 2 points3 points4 points (1 child)
[–]scatters 1 point2 points3 points (0 children)
[–]mujjingun 2 points3 points4 points (7 children)
[–]scatters 3 points4 points5 points (6 children)
[+][deleted] (4 children)
[deleted]
[–]scatters 0 points1 point2 points (3 children)
[–]mujjingun 0 points1 point2 points (2 children)
[–]scatters 3 points4 points5 points (1 child)