What you need is the address of the data... which would look like
function2(& program2->function().value(), 1);
and would be perfectly safe, since the lifetime of the temporary variable is until the end of the complete expression, which is long enough1 for function2
to use it.
Unfortunately, you can't use the &
address-of operator on an rvalue. But there is a workaround, since you can bind a const
lvalue reference to an rvalue, and you want a pointer to const
data anyway:
template<T>
const T* get_addr(const T& v) { return &v; }
function2( get_addr(program2->function().value()), 1 );
If you get a signed-vs-unsigned, mismatch, try
function2( get_addr<char>(program2->function().value()), 1 );
Of course this is a lot of work just to avoid giving the variable a name, as Captain Obvlious has done.
1 Unless function2
saves that pointer for later, but if it does then you shouldn't be using a local variable to hold the value either, but pay very close attention to object lifetime.