Namespaces
Variants
Views
Actions

std::reference_converts_from_temporary

From cppreference.com
< cpp‎ | types
 
 
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
(C++11)
(C++11)
reference_converts_from_temporary
(C++23)

Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)(deprecated in C++23)
(C++11)(deprecated in C++23)
(C++11)
(C++11)
(C++17)

(C++11)(until C++20)(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
 
Defined in header <type_traits>
template< class T, class U >
struct reference_converts_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_converts_from_temporary or reference_converts_from_temporary_v is undefined.

Contents

[edit] Helper variable template

template< class T, class U >

inline constexpr reference_converts_from_temporary_v =

    std::reference_converts_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 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>

[edit] Notes

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

[edit] Example

#include <type_traits>
#include <iostream>
 
int main()
{
    std::cout << std::boolalpha
        << std::reference_converts_from_temporary_v<int&&, int> << '\n'
        << std::reference_converts_from_temporary_v<const int&, int> << '\n'
        << std::reference_converts_from_temporary_v<int&&, int&&> << '\n'
        << std::reference_converts_from_temporary_v<const int&, int&&> << '\n'
        << std::reference_converts_from_temporary_v<int&&, long&&> << '\n';
        << std::reference_converts_from_temporary_v<int&&, long> << '\n';
}

Output:

true
true
false
false
true
true

[edit] See also

checks if a type can be converted to the other type
(class template) [edit]
(C++17)(C++23)
invokes any Callable object with given arguments and possibility to specify return type (since C++23)
(function template) [edit]
(C++11)
binds one or more arguments to a function object
(function template) [edit]
(C++17)
calls the provided functor with the arguments held by one or more variants
(function template) [edit]
constructs a new std::function instance
(public member function of std::function<R(Args...)>) [edit]
constructs a new std::move_only_function object
(public member function of std::move_only_function<R(Args...) cv ref noexcept(noex)>) [edit]
constructs the task object
(public member function of std::packaged_task<R(Args...)>) [edit]