home | help
deductiongu...forstd::span(3)  C++ Standard Libary deductiongu...forstd::span(3)

NAME
     deductionguidesforstd::span - deductionguidesforstd::span

Synopsis
	Defined in header <span>
	template<	 class	      It,	 class	      EndOrSize        >
     (since
	span(		  It,		  EndOrSize		)	      ->
     C++20)
	span<std::remove_reference_t<std::iter_reference_t<It>>>;
     (until
										     C++26)
	template< class It, class EndOrSize >

	span(		  It,		  EndOrSize		)	      ->
     (since
	span<std::remove_reference_t<std::iter_reference_t<It>>,
     C++26)

				      /*maybe-static-ext*/<EndOrSize>>;      (1)
	template<	 class	      T,	 std::size_t	     N	       >
     (2) (since
	span(	     T	      (&)[N]	    )	     ->        span<T,	     N>;
     C++20)
	template<	 class	      T,	 std::size_t	     N	       >
     (3) (since
	span(	    std::array<T,	N>&	 )	->	span<T,      N>;
     C++20)
	template<	 class	      T,	 std::size_t	     N	       >
     (4) (since
	span(	 const	  std::array<T,    N>&	  )   ->   span<const	T,   N>;
     C++20)
	template<		   class		   R		       >
     (since
	span(			  R&&			 )		      ->
     (5) C++20)
	span<std::remove_reference_t<std::ranges::range_reference_t<R>>>;

	The following deduction guides are provided for span.

	1) Allows the element type to  be  deduced  from  the  iterator-sentinel
     pair.
	It also allows the static extent to be deduced if EndOrSize satisfies
	integral-constant-like.
	(since C++26). This overload participates in overload resolution only if
     It
	satisfies contiguous_iterator.
	2-4)  Allows  the  static  extent to be deduced from built-in arrays and
     std::array.
	5) Allows the element type to be deduced from ranges. This overload par-
     ticipates in
	overload resolution only if R satisfies contiguous_range.

Example
     // Run this code

      #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 a sizeof a extent\n"
			   "aaaaaa1/4aaaaaaaaa1/4aaaaaaaa\n";
	      return;
	  }
	  std::cout << std::setw(4) << rem << " a " << std::setw(6)  <<  size_of
     << " a ";
	  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 a sizeof a extent
      aaaaaa1/4aaaaaaaaa1/4aaaaaaaa
	s1 a	 16 a dynamic
	s2 a	 16 a dynamic
	s3 a	  8 a 2
	s4 a	  8 a 5
	s5 a	 16 a dynamic
	s6 a	  8 a 3
	s7 a	  8 a 3
	s8 a	 16 a dynamic

http://cppreference.com 	   2024.06.10	   deductiongu...forstd::span(3)

home | help