Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting arrays in stl like copy

Tags:

c++

copy

stl

vector

It's time for another 'how do i do this in c++ without loosing my grip'-question!

This time:

Considering the following code taken from cplusplus.com:

template<class InputIterator, class OutputIterator>
  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
{
  while (first!=last) *result++ = *first++;
  return result;
}

Is there a way to cast *first to the type of *result?

In other words: is there a way to determine (at compiletime) the type of result?

like image 632
fho Avatar asked Dec 06 '25 20:12

fho


1 Answers

yes, the type of *result is ('cause the type of result is OutputIterator )

typename std::iterator_traits<OutputIterator>::value_type

of course, if OutputIterator is a pointer or a correctly written STL-compatible iterator. Otherwise, no, I think there is no way. In the upcoming C++0x, it would be much easier, that is,

decltype(*result)

HTH,

Armen

like image 93
Armen Tsirunyan Avatar answered Dec 08 '25 12:12

Armen Tsirunyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!