std::refere...m_temporary(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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: if T is an lvalue reference type to a const- but not volatile-qualified object type, or an rvalue reference type, then both std::remove_reference_t<T> and std::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 for std::reference_converts_from_temporary or std::reference_converts_from_temporary_v, the behavior is undefined. please do not. 🫢

πŸ‘‘ Member constants πŸ‘‘

  • value β€” 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. it's a public static member constant ✨

🧚 Member functions 🧚

  • operator bool β€” converts the object to bool, returns value (public member function)
  • operator() β€” returns value (C++14) (public member function)

πŸ¦‹ Member types πŸ¦‹

  • 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. 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::function instance (public member function of std::function<R(Args...)>)
  • constructor (C++23) β€” constructs a new std::move_only_function object (public member function of std::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