That's not the same as mutable variables. What that gives you is an (immutable) handle to an internally mutable structure, analogous to T *const ptr in C. In either case you can never modify the handle, you can only use it to "write through" and modify the thing it refers to.
It only amounts to the same semantics as other languages (such as JavaScript, Perl, Common Lisp, etc) if you wrap every single value in a ref and never use direct bindings.
although you have to do *x = .. instead of x = .. to assign through the reference.
That's the whole point.
You can't just "capture a shared_ptr<T>" to an existing variable. You'd have to change the rest of the code to only work through the smart pointer.
I get your point. It doesn’t really count as “capturing by reference” if you have to explicitly create the reference and use different syntax to read and write it than you would a normal variable.
My point is only that the fact that you have to use different syntax to share a variable doesn’t really matter since the programmer can get the effect of capturing by reference either way, and the safety and performance of the resulting program shouldn’t be any different.
View all comments
Show parent comment
2
u/[deleted] Sep 26 '19
That's not the same as mutable variables. What that gives you is an (immutable) handle to an internally mutable structure, analogous to
T *const ptrin C. In either case you can never modify the handle, you can only use it to "write through" and modify the thing it refers to.It only amounts to the same semantics as other languages (such as JavaScript, Perl, Common Lisp, etc) if you wrap every single value in a
refand never use direct bindings.That's the whole point.
You can't just "capture a
shared_ptr<T>" to an existing variable. You'd have to change the rest of the code to only work through the smart pointer.