std::uninitialized_copy_n

Header: <memory>

  1. Copies count elements from a range beginning at first to an uninitialized memory area beginning at d_first as if by for (; count > 0; ++d_first, (void) ++first, –count) ::new (voidify(*d_first)) typename std::iterator_traits::value_type(*first);

# Declarations

template< class InputIt, class Size, class NoThrowForwardIt >
NoThrowForwardIt uninitialized_copy_n( InputIt first, Size count,
NoThrowForwardIt d_first );

(since C++11) (constexpr since C++26)

template< class ExecutionPolicy, class ForwardIt,
class Size, class NoThrowForwardIt >
NoThrowForwardIt uninitialized_copy_n( ExecutionPolicy&& policy,
ForwardIt first, Size count,
NoThrowForwardIt d_first );

(since C++17)

# Parameters

# Return value

Iterator to the element past the last element copied.

# Notes

Feature-test macro Value Std Feature __cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr for specialized memory algorithms, (1)

# Example

#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
 
int main()
{
    std::vector<std::string> v = {"This", "is", "an", "example"};
 
    std::string* p;
    std::size_t sz;
    std::tie(p, sz) = std::get_temporary_buffer<std::string>(v.size());
    sz = std::min(sz, v.size());
 
    std::uninitialized_copy_n(v.begin(), sz, p);
 
    for (std::string* i = p; i != p + sz; ++i)
    {
        std::cout << *i << ' ';
        i->~basic_string<char>();
    }
    std::cout << '\n';
 
    std::return_temporary_buffer(p);
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2133C++98the effect description used a for loop with the iterationexpression ++d_first, ++first, –count, whichresults in argument-dependent lookups of operator,discards the valueof one operand todisable those ADLs
LWG 2433C++11this algorithm might be hijacked by overloaded operator&uses std::addressof
LWG 3870C++20this algorithm might create objects on a const storagekept disallowed

# See also