deductiongu...rstd::array(3)

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

πŸ’– NAME πŸ’–

okay bestie, this is deductiongu...rstd::array(3), a C++ Standard Libary page about one very specific deduction guide β€” yes, the title is long enough to need its own emotional support water bottle 😭. It's giving template arcana, but make it cute πŸŽ€.

✨ Synopsis ✨

First up: this is defined in header <array> πŸ’…βœ¨

template< class T, class... U >

array( T, U... )                  (since C++17)

  -> array<T, 1 + sizeof...(U)>;

So the tea is: one deduction guide is provided for std::array, and it exists to give you an equivalent of std::experimental::make_array for construction of std::array from a variadic parameter pack. slay. 🫢

⚠️ technical caveat, no cap: the program is ill-formed if (std::is_same_v<T, U> && ...) is not true. Also note that (std::is_same_v<T, U> && ...) is true when sizeof...(U) is zero. That's literally the rule, bestie. πŸ’€

🌈 Example 🌈

Run this code β€” it's the example, and it ate:

#include <array>
#include <cassert>

int main()
{
    int const x = 10;
    std::array a{1, 2, 3, 5, x}; // OK, creates std::array<int, 5>
    assert(a.back() == x);

//  std::array b{1, 2u}; // Error, all arguments must have the same type

//  std::array<short> c{3, 2, 1}; // Error, wrong number of template args
    std::array c(std::to_array<short>({3, 2, 1})); // C++20 alternative,
                                                   // creates std::array<short, 3>
}

mwah πŸ’‹ bye now β€” source/version: http://cppreference.com, date: 2024.06.10, manual page: deductiongu...rstd::array(3). go be a template icon πŸ‘‘βœ¨

πŸ“¦ where this page comes from