4 ms·
Although I've never found it useful, I believe if you do TYPE *const ptr = foo(); to initialize a C pointer where you can't change what thing it points to
by jstimpfle 5y ago
Although I've never found it useful, I believe if you do
TYPE *const ptr = foo();
to initialize a C pointer where you can't change what thing it points to (as opposed to the thing itself), then you get 100% of the alleged safety benefits that you get from C++ references.
Except, you're not adding another type category in the type system, with all the complexities (non-orthogonality of features) that come with that.
Granted, compared to C++ reference declarations like
TYPE& ptr = foo();
you'll have to type a few characters more, but then again you won't have to repeat your method signatures in 18 different flavours.
- pjmlp 5y agoOn those cases not, but certainly as function parameters. or class members that are initialized on the constructor and never change during the class lifetime, if they are pointers instead it may raise lifetime questions. Althought probably the correct way would be to make use of std::observer_ptr<>(), but it still isn't in the standard.