deductiongu...forstd::span(3)
π NAME π
okay so bestie, this is the C++ Standard Libary page deductiongu...forstd::span(3) β yes, the slug is giving keyboard-smash couture and we are obsessed π
. The NAME line is literally deductionguidesforstd::span - deductionguidesforstd::span. no cap, that's the whole thing. π
β¨ Synopsis β¨
Defined in header <span> π¦
The following deduction guides are provided for span β it's the deduction-guide squad, fr fr π:
guide (1) β It + EndOrSize, until C++26 πΈ
template< class It, class EndOrSize >
span( It, EndOrSize ) ->
span<std::remove_reference_t<std::iter_reference_t<It>>>;
(1) Β· (since C++20) Β· (until C++26) β¨
guide (1) β It + EndOrSize, since C++26 π₯
template< class It, class EndOrSize >
span( It, EndOrSize ) ->
span<std::remove_reference_t<std::iter_reference_t<It>>,
/*maybe-static-ext*/<EndOrSize>>;
(1) Β· (since C++26) β¨
guide (2) β built-in array π
template< class T, std::size_t N >
span( T (&)[N] ) -> span<T, N>;
(2) Β· (since C++20) β¨
guide (3) β std::array<T, N>& π
template< class T, std::size_t N >
span( std::array<T, N>& ) -> span<T, N>;
(3) Β· (since C++20) β¨
guide (4) β const std::array<T, N>& π§
template< class T, std::size_t N >
span( const std::array<T, N>& ) -> span<const T, N>;
(4) Β· (since C++20) β¨
guide (5) β ranges π
template< class R >
span( R&& ) ->
span<std::remove_reference_t<std::ranges::range_reference_t<R>>>;
(5) Β· (since C++20) β¨
The following deduction guides are provided for span:
(1)β lets the element type be deduced from the iterator-sentinel pair. It also lets the static extent be deduced ifEndOrSizesatisfiesintegral-constant-like(since C++26). This overload participates in overload resolution only ifItsatisfiescontiguous_iterator.2-4)β lets the static extent be deduced from built-in arrays andstd::array.5)β lets the element type be deduced from ranges. This overload participates in overload resolution only ifRsatisfiescontiguous_range.
π Example π
// Run this code π» β literally press the vibe check and watch it print:
#include <array>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <span>
#include <string_view>
#include <vector>
void print(std::string_view rem = "", std::size_t size_of = 0,
std::size_t extent = 0)
{
if (rem.empty())
{
std::cout << "name β sizeof β extent\n"
"ββββββΌβββββββββΌββββββββ\n";
return;
}
std::cout << std::setw(4) << rem << " β " << std::setw(6) <<
size_of << " β ";
if (extent == std::dynamic_extent)
std::cout << "dynamic";
else
std::cout << extent;
std::cout << '\n';
}
int main()
{
int a[]{1, 2, 3, 4, 5};
print();
std::span s1{std::begin(a), std::end(a)}; // guide (1)
print("s1", sizeof s1, s1.extent);
std::span s2{std::begin(a), 3}; // guide (1)
print("s2", sizeof s2, s2.extent);
#if __cplusplus > 202302L
std::span s3{std::begin(a), std::integral_constant<std::size_t, 2>{}}; // guide (1)
print("s3", sizeof s3, s3.extent);
#endif // C++26
std::span s4{a}; // guide (2)
print("s4", sizeof s4, s4.extent);
std::span<int> s5{a}; // does not use a guide, makes a dynamic span
print("s5", sizeof s5, s5.extent);
std::array arr{6, 7, 8};
std::span s6{arr}; // guide (3)
print("s6", sizeof s6, s6.extent);
s6[0] = 42; // OK, element_type is 'int'
const std::array arr2{9, 10, 11};
std::span s7{arr2}; // guide (4)
print("s7", sizeof s7, s7.extent);
// s7[0] = 42; // Error: element_type is 'const int'
std::vector v{66, 69, 99};
std::span s8{v}; // guide (5)
print("s8", sizeof s8, s8.extent);
}
π¦ Possible output π¦
name β sizeof β extent
ββββββΌβββββββββΌββββββββ
s1 β 16 β dynamic
s2 β 16 β dynamic
s3 β 8 β 2
s4 β 8 β 5
s5 β 16 β dynamic
s6 β 8 β 3
s7 β 8 β 3
s8 β 16 β dynamic
okay bestie that's the whole page π mwah, go deduce something iconic. source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: deductiongu...forstd::span(3) ππ
π¦ 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