std::refere...m_temporary(3)

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

πŸ’– std::reference_constructs_from_temporary πŸ’–

okay so this page is from the C++ Standard Libary manual, and the main character is std::reference_constructs_from_temporary β€” yes, the whole name, no nickname, we respect the icon era. πŸ’…

🌸 NAME 🌸

std::reference_constructs_from_temporary β€” std::reference_constructs_from_temporary. That's it, that's the name, and yes it's a long queen of a name, we love that for her. πŸŽ€

✨ Synopsis ✨

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

Let V be std::remove_cv_t<U> if U is a scalar type or cv void, or U otherwise. If T is a reference type, and given a hypothetic expression e such that decltype(e) is V, 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. πŸ¦‹

If T is an lvalue reference type to a const- but not volatile-qualified object type or an rvalue reference type, 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. (Yes, β€œan arrays” is literally in here β€” we keep every detail, bestie. 😭)

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_constructs_from_temporary or std::reference_constructs_from_temporary_v, the behavior is undefined. Do not do that. Periodt. πŸ’€

πŸ“ Member constants

  • value β€” 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 [static] 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

  • Type β€” Definition
  • value_type β€” bool
  • type β€” std::integral_constant<bool, value> πŸ¦‹

πŸ‘‘ Notes

std::reference_constructs_from_temporary can be used for rejecting some cases that always produce dangling references. That's the whole point, bestie β€” no dangling-ref heartbreak on our watch. 😭

It is also possible to use member initializer list to reject binding a temporary object to a reference if the compiler has implemented CWG1696. Noted, iconic compiler behavior. 🫢

πŸ”₯ Example

// Run this code

#include <type_traits>

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

int main() {}

🌟 See also

  • is_constructible β€” checks if a type has a constructor for specific arguments (C++11) (class template) 🌸
  • is_trivially_constructible β€” checks if a type has a constructor for specific arguments (C++11) (class template) 🌸
  • is_nothrow_constructible β€” checks if a type has a constructor for specific arguments (C++11) (class template) 🌸
  • constructor β€” constructs a new tuple (public member function of std::tuple<Types...>) πŸŽ€
  • constructor β€” constructs new pair (public member function of std::pair<T1,T2>) πŸŽ€
  • make_from_tuple β€” construct an object with a tuple of arguments (C++17) (function template) πŸ¦‹

πŸ’– xoxo, this page came from http://cppreference.com, version/date 2024.06.10, and the page tag is std::refere...m_temporary(3). Stay slay, bestie. πŸ’…βœ¨

πŸ“¦ where this page comes from