std::default_initializable(3)

πŸ“¦
version 2024.07.05 Β· 3 versions
, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

okay bestie, welcome to the std::default_initializable(3) page from the C++ Standard Libary manual 🫢 and the name entry is literally std::default_initializable β€” std::default_initializable (yes, twice, and honestly? iconic).


✨ Synopsis ✨

It’s defined in header <concepts>, and it’s been a thing since C++20. The synopsis is giving exactly this:

Defined in header <concepts>
template< class T >

concept default_initializable = std::constructible_from<T> &&
(since C++20)
requires { T{}; } &&

                                        /* T t; is well-formed, see below */;

So what does the default_initializable concept actually check? It checks whether variables of type T can be:

  • value-initialized (T() is well-formed);
  • direct-list-initialized from an empty initializer list (T{} is well-formed); and
  • default-initialized (T t; is well-formed).

And this part is important, no cap: access checking is performed as if in a context unrelated to T. Only the validity of the immediate context of the variable initialization is considered. πŸ’…


πŸ”₯ Possible implementation πŸ”₯

Here’s one possible implementation, and yes it’s lowkey minimal:

template<class T>
concept default_initializable =
    std::constructible_from<T> &&
    requires { T{}; } &&
    requires { ::new T; };

πŸ‘€ See also πŸ‘€

  • constructible_from (C++20) β€” specifies that a variable of the type can be constructed from or bound to a set of argument types (concept).
  • is_default_constructible (C++11) β€” checks if a type has a default constructor (class template).
  • is_trivially_default_constructible (C++11) β€” checks if a type has a default constructor (class template).
  • is_nothrow_default_constructible (C++11) β€” checks if a type has a default constructor (class template).

mwah πŸ’‹ that’s the page! source/version/date vibes: http://cppreference.com, 2024.06.10, page std::default_initializable(3). bye bestie 🫢

πŸ“¦ where this page comes from