Have you ever been on a speed date? Me neither. So I’m scared as hell, but the table is waiting and…
Three, two, one. The first one arrives.
int i = 42;
const int& a = i;
const int& b = +i;
i = 100;
std::cout << a << " " << b << '\n';
100 100? Of course not. The second reference initialization creates a temporary object that b refers to. The object has no name of its own in our code, but it’s there, quietly living its extended lifetime.

So the correct answer is 100 42.
And if you’re wondering, the lifetime of the temporary is extended to match the lifetime of the reference.*
I’d love to stay and talk more about it, but…
Three, two, one. The second one arrives.
int i = 42;
const int& a = ++i;
const int& b = i++;
This should be easy by now. Once you know that temps are there, it’s obvious that the result is 43 100.
Shit. Got it wrong again. The other way around. 100 43.

The first reference refers to i itself, while the second refers to another invisible temporary. ++i returns an lvalue, while i++ takes a snapshot of i before incrementing it.
And that snapshot is already 43, not 42.
And just in case you need some “impress me with your knowledge” code for your next date, you can simply do this:
++i = 200;
It’s a perfectly valid C++ expression that first increments i by one and then assigns 200 to it.
Shit (again). We’ve spent so much time on this one that the next date is already waiting.
Three, two.. Just sit.
int i = 42;
decltype(auto) a = i;
decltype(auto) b = (i);
i = 100;
std::cout << a << " " << b << '\n';
42 100.
The first is a copy, and the second is a reference. A single pair of parentheses makes all the difference, thanks to the special rules of decltype. And it makes much more sense if you look at it this way:
decltype(i) // intdecltype((i)) // int&
Not sure about you, but to me, this still looks suspicious. Luckily, we don’t get the same behaviour with plain auto. Only the third declaration creates a reference:
auto a = i;
auto b = (i);
auto& c = i;
Time’s up. Switch tables.
let mut i = 42;
let a = i;
let b = &i;
i = 100;
println!("{a} {b}");
The result? A compilation error. You can’t just mutate a variable while someone else is still using an immutable borrow of it.
Three, two, one.
let mut i = 42;
let a = i;
let b = &mut i;
*b = 100;
println!("{a} {b}");
Now, this one is easy. So instead of explaining it, we can finally have a sip of wine, Coke, or whatever is on the Rust table and just wait for the next one to arrive.
And if you haven’t noticed that we’ve switched programming languages, maybe you’ve had enough wine already.
Last one. I am tired of writing. You are tired of reading.
use std::cell::Cell;
let i = Cell::new(42);
let a = i.get();
let b = &i;
i.set(100);
println!("{} {}", a, b.get());
What happens here? Instead of borrowing an integer directly, we put it in a little box called Cell and give b a shared reference to that box.
Notice that neither i nor the reference is declared as mutable, and yet everything compiles and works.
42 100.
This little trick is called interior mutability. The box allows us to change its contents even through a shared reference.
Time to go home. I hope you didn’t mind that all the dishes were served by the Toaster. Dishes? You thought speed dating was about meeting people?
Well, we did meet, didn’t we?
* Yes, all you Rust lovers — we do have lifetimes in C++. We just don’t shout about them.
You must be logged in to post a comment.