std::reference_constructs_from_temporary

Header: <type_traits>

Let V be std::remove_cv_t if U is a scalar type or cv void, or U otherwise. If T is a reference type, and given a hypothetic expression e such that decltype(e) is V, the variable definition T ref(e); is well-formed and binds a temporary object to ref, then provides the member constant value equal to true. Otherwise, value is false.

# Declarations

template< class T, class U >
struct reference_constructs_from_temporary;

(since C++23)

# Notes

std::reference_constructs_from_temporary can be used for rejecting some cases that always produce dangling references.

It is also possible to use member initializer list to reject binding a temporary object to a reference if the compiler has implemented CWG1696.

# Example

#include <type_traits>
 
static_assert(std::reference_constructs_from_temporary_v<int&&, int> == true);
static_assert(std::reference_constructs_from_temporary_v<const int&, int> == true);
static_assert(std::reference_constructs_from_temporary_v<int&&, int&&> == false);
static_assert(std::reference_constructs_from_temporary_v<const int&, int&&> == false);
static_assert(std::reference_constructs_from_temporary_v<int&&, long&&> == true);
static_assert(std::reference_constructs_from_temporary_v<int&&, long> == true);
 
int main() {}

# See also