C++11 copy_n should return a pair<InputIterator,OutputIterator>, notjust OutputIterator

R

Ricky65

Reading N3291 I noticed that algorithm copy_n is defined as:

"template<class InputIterator, class Size, class OutputIterator>
OutputIterator copy_n(InputIterator first, Size n, OutputIterator
result);"

copy_n should return a pair - an OutputIterator in the destination
range and also an InputIterator to the last element copied in the
input range. In "Elements Of Programming" by Alexander Stepanov and
Paul McJones copy_n is in this form.

By just returning an OutputIterator copy_n violates the idiom - "an
algorithm should return all the information it computed". To quote
Alexander Stepanov, "Throwing away useful information (or returning
redundant information) usually indicates a poorly designed interface."

Far more useful copy_n which returns a
std::pair<InputIterator,OutputIterator>:
"template<class InputIterator, class Size, class OutputIterator>
std::pair<InputIterator, OutputIterator> copy_n(InputIterator first,
Size n, OutputIterator result);"

Kind regards

Riccardo Marcangelo
 
R

Ricky65

Correction to above. I meant an InputIterator one past the last
element copied in the
input range.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top