Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::ranges::destroy_n(3)     C++ Standard Libary    std::ranges::destroy_n(3)

NAME
       std::ranges::destroy_n -	std::ranges::destroy_n

Synopsis
	  Defined in header <memory>
	  Call signature
	  template< no-throw-input-iterator I >

	  requires		       std::destructible<std::iter_value_t<I>>
       (since C++20)

	  constexpr I destroy_n( I first, std::iter_difference_t<I> n )
	  noexcept;

	  Destroys the n objects in the	range starting at first, equivalent to

	return std::ranges::destroy(std::counted_iterator(first, n),  std::de-
       fault_sentinel).base();

	  The  function-like  entities	described  on this page	are niebloids,
       that is:

	    * Explicit template	argument lists may not be specified when call-
       ing any of them.
	    * None of them is visible to argument-dependent lookup.
	    * When one of them is found	by normal unqualified lookup  for  the
       name to the left
	      of  the  function-call  operator,	it inhibits argument-dependent
       lookup.

	  In practice, they may	be implemented as function  objects,  or  with
       special compiler
	  extensions.

Parameters
	  first	- the beginning	of the range of	elements to destroy
	  n	- the number of	elements to destroy

Return value
	  The end of the range of objects that has been	destroyed.

Complexity
	  Linear in n.

Possible implementation
	  struct destroy_n_fn {
	    template<no-throw-input-iterator I>
	      requires std::destructible<std::iter_value_t<I>>
	    constexpr I	operator()(I first, std::iter_difference_t<I> n) const
       noexcept
	    {
	      for (; n != 0; (void)++first, --n)
		std::ranges::destroy_at(std::addressof(*first));
	      return first;
	    }
	  };

	  inline constexpr destroy_n_fn	destroy_n{};

Example
	  The  following  example demonstrates how to use ranges::destroy_n to
       destroy a
	  contiguous sequence of elements.

       // Run this code

	#include <memory>
	#include <new>
	#include <iostream>

	struct Tracer {
	    int	value;
	    ~Tracer() {	std::cout << value << "	destructed\n"; }
	};

	int main()
	{
	    alignas(Tracer) unsigned char buffer[sizeof(Tracer)	* 8];

	    for	(int i = 0; i <	8; ++i)
		new(buffer + sizeof(Tracer) * i)  Tracer{i};  //manually  con-
       struct objects

	    auto ptr = std::launder(reinterpret_cast<Tracer*>(buffer));

	    std::ranges::destroy_n(ptr,	8);
	}

Output:
	0 destructed
	1 destructed
	2 destructed
	3 destructed
	4 destructed
	5 destructed
	6 destructed
	7 destructed

See also
	  ranges::destroy_at destroys an object	at a given address
	  (C++20)	     (niebloid)
	  ranges::destroy    destroys a	range of objects
	  (C++20)	     (niebloid)
	  destroy_n	     destroys a	number of objects in a range
	  (C++17)	     (function template)

http://cppreference.com		  2022.07.31	     std::ranges::destroy_n(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::ranges::destroy_n&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help