From cppreference.com
template<classTo,classFrom>structreference_constructs_from_temporary;(since C++23)Let V be std::remove_cv_t<From> if From is a scalar type or
void, or From otherwise. If To is a reference type, and given a hypothetical expression e such that decltype(e) is V, the variable definition Toref(e); is well-formed and
to ref, then provides the member constant value equal to true. Otherwise, value is false.
If To is an lvalue reference type to a const- but not volatile-qualified object type or an rvalue reference type, both std::remove_reference_t<To> and std::remove_reference_t<From> must be
,
void, or an
; otherwise the behavior is undefined.
If an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.
If the program adds specializations for std::reference_constructs_from_temporary or std::reference_constructs_from_temporary_v, the behavior is undefined.
Helper variable template
template<classTo,classFrom>constexprboolreference_constructs_from_temporary_v=std::reference_constructs_from_temporary<To,From>::value;(since C++23)Inherited from
Member constants
value
[static]
true if To is a reference type, a From value can be bound to To in direct-initialization, and a temporary object would be bound to the reference, false otherwise
(public static member constant)Member functions
operator bool
converts the object to bool, returns value
(public member function)operator()
(C++14)
returns value
(public member function)Member types
Type Definition value_typebooltypestd::integral_constant<bool,value>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
.
Example
Run this code
#include<type_traits>static_assert(std::reference_constructs_from_temporary_v<int&&,int>&&std::reference_constructs_from_temporary_v<constint&,int>&&!std::reference_constructs_from_temporary_v<int&&,int&&>&&!std::reference_constructs_from_temporary_v<constint&,int&&>&&std::reference_constructs_from_temporary_v<int&&,long&&>&&std::reference_constructs_from_temporary_v<int&&,long>);structS{operatorint()const;explicitoperatorconstint&()const;};// For reference construction (direct init), explicit conversions are considered,// so `operator const int&` is found, which is not classified as conversion from a temporarystatic_assert(!std::reference_constructs_from_temporary_v<constint&,S>);// For reference conversion (copy init), explicit conversions are disregarded,// so `operator int` is found, which produces a temporary to which a `const int&` would bindstatic_assert(std::reference_converts_from_temporary_v<constint&,S>);structBad{explicitoperatorint()const{return{};}};structFine{explicitoperatorconstint&()const{staticintt;returnt;}};template<typenameT>structWrapper{Tt;template<typenameTT>Wrapper(TT&&tt):t(tt)// The construction of T is done here...{// ... So if the conversion from TT to T results in a temporary,// that temporary will be destroyed when this function returns// I.e., it is guaranteed that t will be danglingstatic_assert(!std::reference_constructs_from_temporary_v<T,TT>,"Unconditionally dangling reference caused by explicit conversion");}};intmain(){// Wrapper<const int&> w_bad(Bad{});Wrapper<constint&>w_fine(Fine{});}See also
is_constructibleis_trivially_constructibleis_nothrow_constructible
(C++11)(C++11)(C++11)
checks if a type has a constructor for specific arguments
(class template)
constructs a new tuple
(public member function of std::tuple<Types...>)
constructs new pair
(public member function of std::pair<T1,T2>)
(C++17)
construct an object with a tuple of arguments
(function template)
reference_converts_from_temporary
(C++23)
checks if a reference is bound to a temporary in
(class template)
reference_constructs_from_temporary
(C++26)
checks if a reference is bound to a temporary in
(function)