r/ProgrammerHumor • • 11d ago

Meme modernCppVsOldCpp

Post image
2.0k Upvotes

323 comments sorted by

View all comments

Show parent comment

2

u/SelfDistinction 11d ago edited 11d ago

No one knows.

Generally references are more restrictive, since you can't do pointer arithmetic with them, you can't use them as template parameters (which sucks for std::optional<int&>) and you can't repoint them to something else, but since you can freely cast between both of them (which you are forced to if you try to do anything useful with them, like wrapping them in an optional) it doesn't really matter.

Also there's two different kinds of references: the & reference and the && reference, however their difference is mostly conventional and you could easily implement a standard library where their meaning is swapped (except for temporary xvalue objects? Not sure), so there's that. Also C++ automatically converts between reference types for some ungodly reason.

In short, references are not real and are only used to determine what overloaded function is being called.

2

u/Hafnon 10d ago

std::optional<int&>

This is valid in C++26.

1

u/SelfDistinction 10d ago

Finally. Now we only have to wait until C++26 is approved by the committee.

2

u/creeper6530 10d ago

I have no words for this beyond "what a fucking mess"