std::reference_converts_from_temporary - cppreference.com
Namespaces
Variants

std::reference_converts_from_temporary

From cppreference.com
 
 
 
Type traits
Type traits (since C++11)
Type categories
Type properties
(C++14)
(deprecated in C++26)
(until C++20*)
(deprecated in C++20)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(deprecated in C++23)
(deprecated in C++23)
(until C++20*)(C++17)
 
Defined in header <type_traits>
template< class To, class From >
struct reference_converts_from_temporary;
(since C++23)

Let V be std::remove_cv_t<From> if From is a scalar type or cv 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 To 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.

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 complete types, cv void, or an arrays of unknown bound; 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_converts_from_temporary or std::reference_converts_from_temporary_v, the behavior is undefined.

Helper variable template

template< class To, class From >
constexpr bool reference_converts_from_temporary_v =
    std::reference_converts_from_temporary<To, From>::value;
(since C++23)

Inherited from std::integral_constant

Member constants

value
[static]
true if To is a reference type, a From value can be bound to To in copy-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_type bool
type std::integral_constant<bool, value>

Notes

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

Example

#include <type_traits>

static_assert
(""
    && std::reference_converts_from_temporary_v<int&&, int> == true
    && std::reference_converts_from_temporary_v<const int&, int> == true
    && std::reference_converts_from_temporary_v<int&&, int&&> == false
    && std::reference_converts_from_temporary_v<const int&, int&&> == false
    && std::reference_converts_from_temporary_v<int&&, long&&> == true
    && std::reference_converts_from_temporary_v<int&&, long> == true
);

int main() {}

See also