cpp / latest / types / reference_constructs_from_temporary.html /

std::reference_constructs_from_temporary

Defined in header <type_traits>
template< class T, class U >
struct reference_constructs_from_temporary;
(since C++23)

If.

  • T is a reference type, and
  • given the hypothetic function declared as U makeval();, the variable definition T ref(makeval()); is well-formed and binds a temporary object to ref,

provides the member constant value equal to true. Otherwise value is false.

If T is a reference type, both std::remove_reference_t<T> and U shall 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.

The behavior of a program that adds specializations for reference_constructs_from_temporary or reference_constructs_from_temporary_v is undefined.

Helper variable template

template< class T, class U >
inline constexpr reference_constructs_from_temporary_v =
    std::reference_constructs_from_temporary<T, U>::value;
(since C++23)

Inherited from std::integral_constant

Member constants

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

Notes

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>
#include <iostream>
 
int main()
{
    std::cout << std::boolalpha
        << std::reference_constructs_from_temporary_v<int&&, int> << '\n'
        << std::reference_constructs_from_temporary_v<const int&, int> << '\n'
        << std::reference_constructs_from_temporary_v<int&&, int&&> << '\n'
        << std::reference_constructs_from_temporary_v<const int&, int&&> << '\n'
        << std::reference_constructs_from_temporary_v<int&&, long&&> << '\n';
        << std::reference_constructs_from_temporary_v<int&&, long> << '\n';
}

Output:

true
true
false
false
true
true

See also

(C++11)(C++11)(C++11)
checks if a type has a constructor for specific arguments
(class template)
(C++11)
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)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/types/reference_constructs_from_temporary