iouvxz
iouvxz

Reputation: 163

Access the rvalue address using an operator?

How can I use an expression or an operator to get the address of a rvalue ?

char buffer[100];
time_t rawtime=time(nullptr); //rawtime used only once ,after that it's abandoned.
strftime(buffer, 80, "%y%m%d %H%M%S", localtime(&rawtime));// rewrite these two lines into one line?

Should act like this:

strftime(buffer, 80, "%y%m%d %H%M%S", localtime(&(time(nullptr))));

Upvotes: 0

Views: 147

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477368

The built-in address-of operator requires an lvalue operand, so you need to produce an lvalue somehow.

You can turn rvalues into lvalues with some kind of opposite of std::move, here called stay:

template <typename T>
T & stay(T && x) { return x; }

Usage:

std::localtime(&stay(std::time(nullptr))

Alternatively, you can use some other pre-existing function template that has a reference parameter and provide an explicit const template argument, since rvalues can bind to constant lvalue references. (Usually this is a very dangerous aspect of such interfaces ("rvalue magnets"), but we'll exploit them here for this use case.) One example function template could be std::min, but we can use std::addressof for even greater convenience here:

#include <memory>

// ...
std::localtime(std::addressof<const std::time_t>(std::time(nullptr))
// ...         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Upvotes: 3

Related Questions