jeudi 26 octobre 2017

Is it legal to pass the address of a temporary to a function with a pointer argument

Is it legal to pass the address of a temporary object to a function that takes a pointer. If not, where in the standard does it say this is UB. Specifically, I would like confirmation that the following code is legal and doesn't contain undefined behavior:

#include <memory>
template <class T>
inline const T* unsafe_addressof(const T&& x) {
    return std::addressof(x);
}

struct S {};

// NOTE: p only used until f returns.
void f(const S* p);

int main() {
    f(unsafe_addressof(S{}));
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire