One possible way is to write your own copy
to bind the object to an lvalue reference:
template <class T>
constexpr T& copy(T&& t) noexcept
{
return t;
}
And you can test it this way:
test = copy(a);
test = copy(3);
test = copy(std::move(a));
You may put this function in your own namespace to keep things clean. You may also choose a better name for it.
To address fear of lifetime issue, here is some considerations:
copy
function takes a reference and returns the same reference immediately. It implies that the caller is responsible for controlling the lifetime.=
.You can static_cast
to const int&
:
test = static_cast<const int&>(3);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With