Re: Dereferencing pointer as argument to function taking reference
On 2013-10-30 01:50, Robert Simpson wrote:
Hi all,
My question is, if I have a function taking an argument by reference,
is it bad practice to call that function with a dereferenced pointer?
E.g.
void myfunc( Widget& w );
std::unique_ptr< Widget > mywidget;
myfunc( *mywidget.get() );
Since this is pseudo-code I need to ask whether this snippet is
intending to demonstrate dereferencing a null pointer and that this
expression is evaluated in the corresponding program? As presented the
answer is "yes" and the following is based on this assumption.
I have written this in my code, but I believe that there is probably
a better solution.
The current specification of C++ does not support any attempt to bind a
dereferenced null pointer to a reference, so the behaviour of that code
is undefined. This is somehow related to an existing core language issue,
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232
but even if the currently suggested wording would be accepted as
written, this would still not allow reference-binding to a null
reference (or "empty lvalue", as denoted in the wording).
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]