From: Seungbeom Kim on
Martin B. wrote:
>
> For me the bottom line so far is that due to the semantics of references
> I will try to use them only for temporary/current-callstack stuff and
> never when I need to keep referencing the thing the reference points to.
> I'll try to give an example:
> void fA(T* p) { // or const*
> // 1) work with p here
> // 2) may also keep p to reference the pointee after fA has been left
> }
> void fB(T& r) { // or const&
> // 1) work with r here
> // 2) do NOT keep a reference (pointer) to r after
> // fB has been left since const& references far too
> // easily bind to temporaries
> }
>
> I think the pointer-parameter does a much better job of signaling to the
> caller that some code may keep referencing the input argument after the
> function returns than does the reference-param :-)

That's a convention that could certainly help in some cases, but
unfortunately it's not very widely accepted. For example, few expect
to write ostream_iterator<T>(&std::cout, "\n") or back_insert_iterator
<V>(&vector) even though the arguments are meant to outlive the function
calls or the returned objects.

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]