std::refere...m_temporary(3)
π NAME π
std::reference_converts_from_temporary β literally the trait that tells you if a reference is about to get stuck holding a temporary π«Ά
ok so real talk, this page lives in the C++ Standard Library manual, section 3, and the whole title got squished down to std::refere...m_temporary(3) in the header because apparently nobody had room. bestie. anyway π
β¨ Synopsis β¨
Defined in header <type_traits>
template< class T, class U > (since C++23)
struct reference_converts_from_temporary;
okay so here's the lore π β let V be std::remove_cv_t<U> if U is a scalar type or cv void, or U otherwise. Then: if T is a reference type, and you take a hypothetic expression e such that decltype(e) is V, and the variable definition T ref = e; is well-formed and binds a temporary object to ref, then it provides the member constant value equal to true. Otherwise, value is false. no cap, that's the whole personality πΈ
β οΈ Okay so this part is important and I need you to actually read it: ifTis an lvalue reference type to a const- but not volatile-qualified object type, or an rvalue reference type, then bothstd::remove_reference_t<T>andstd::remove_reference_t<U>shall be complete types,cv void, or an arrays of unknown bound; otherwise the behavior is undefined. π
β οΈ And also: 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. like, undefined undefined. π
β οΈ And one more, because the standard loves a third warning: if the program adds specializations forstd::reference_converts_from_temporaryorstd::reference_converts_from_temporary_v, the behavior is undefined. please do not. π«Ά
π Member constants π
valueβtrueifTis a reference type, aUvalue can be bound toTin copy-initialization, and a temporary object would be bound to the reference,falseotherwise. it's a public static member constant β¨
π§ Member functions π§
operator boolβ converts the object tobool, returnsvalue(public member function)operator()β returnsvalue(C++14) (public member function)
π¦ Member types π¦
value_typeβbooltypeβstd::integral_constant<bool, value>
π Notes π
std::reference_converts_from_temporary can be used for rejecting some cases that always produce dangling references. main character energy honestly β it's out here saving your code from itself π₯
π Example π
// Run this code
#include <type_traits>
int main() {}
static_assert(std::reference_converts_from_temporary_v<int&&, int> == true);
static_assert(std::reference_converts_from_temporary_v<const int&, int> == true);
static_assert(std::reference_converts_from_temporary_v<int&&, int&&> == false);
static_assert(std::reference_converts_from_temporary_v<const int&, int&&> == false);
static_assert(std::reference_converts_from_temporary_v<int&&, long&&> == true);
static_assert(std::reference_converts_from_temporary_v<int&&, long> == true);
π See also π
is_convertible(C++11) /is_nothrow_convertible(C++20) β checks if a type can be converted to the other type (class template)invoke/invoke_r(C++17) (C++23) β invokes any Callable object with given arguments and possibility to specify return type (function template)bind(C++11) β binds one or more arguments to a function object (function template)visit(C++17) β calls the provided functor with the arguments held by one or more variants (function template)- constructor β constructs a new
std::functioninstance (public member function ofstd::function<R(Args...)>) - constructor (C++23) β constructs a new
std::move_only_functionobject (public member function ofstd::move_only_function) - constructor β constructs the task object (public member function of
std::packaged_task<R(Args...)>)
okay that's the whole page, you ate that π
β¨ this lil beauty came from http://cppreference.com, version 2024.06.10, and yeah the footer still says std::refere...m_temporary(3) because we're keeping it iconic ππ«Ά bye bestie, go bind some references responsibly πΈ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms