Principia icon indicating copy to clipboard operation
Principia copied to clipboard

Allow passing arguments by reference

Open eggrobin opened this issue 10 years ago • 2 comments

The current style conflicts with our avoidance of pointers where the address is unneeded (and the wole "& is visible" argument is fallacious since many things are pointers already, and sometimes we're actually using the pointer).

Decision

Avoid taking the address of a reference wherever possible (exceptions include standard library functions that only return by reference). member variables and contents of containers should not be references (or std::cref, std::ref). Use T& arg instead of not_null<T*> arg when the callee does not need to use the pointer. Note that since we forbid storing the reference, only the pointer may be stored; this means that when a function is called with a reference, the lifetime of the reference is not a concern.

eggrobin avatar Nov 08 '15 13:11 eggrobin

Addendum: WriteToMessage serialization functions should still use not_null<serialization::Whatever*>, since the mutable protobufs accessors return pointers: WriteToMessage(*message.mutable_whatever()) is not an improvement over WriteToMessage(message->mutable_whatever()).

eggrobin avatar Dec 02 '16 16:12 eggrobin

Optional in-out or out arguments should still use (nullable) pointers. Passing &r as an optional in-out argument is allowed even for a reference T& r.

eggrobin avatar Dec 02 '16 18:12 eggrobin