Why is the std :: copy_n interface in C ++ incorrect?

While reading the book ( Notes on programming ) and the current video lecture by Alexander Stepanov, I learned an amazing fact about the influence of good interfaces in the program.

He explained that interface design is very important and has a significant impact on programs. He mentioned the following points when developing interfaces with the following STL algorithm / function "std :: find_if".

template<class I, class P> I find_if (I first, I last, P pred) { while (first!=last) { if (pred(*first)) return first; ++first; } return last; } 

In the above explanation, he explained that

  • do not return "pred" because it is passed in by the caller of this method.
  • do not return "last" because it is known and passed to the calling method. Never return what is known and transmitted by the caller to this function.
  • the "first" should be a return from this method, as during the execution of the method, this could change, so this is something new or an update for the caller, therefore, should be transferred.

So, the main idea that I learned about after analyzing the STL C ++ interfaces:

  • Pass as much information as possible that gives the caller information about the work performed by this particular function.
  • Do not return information that is already known and does not change when the function is executed.

Based on the foregoing, he mentioned that the std :: copy_n interface is incorrect and should be fixed in the near future

WITH

 template< class InputIt, class Size, class OutputIt > OutputIt copy_n( InputIt first, Size count, OutputIt result ); 

TO

 template< class InputIt, class Size, class OutputIt > pair<InputIt, OutputIt> copy_n( InputIt first, Size count, OutputIt result ); 

Can someone explain why it would be pointless and useful for the calling function to get information about the first and the result?

+6
source share

All Articles