Is there any common way to get rid of custom ‘assign’ functor? std::transform could be great but according to c++ standard it prohibits modification of the source elements
The goal is to modify collection elements using as more declarative approach as possible
template <typename T>
struct assign : std::binary_function<T, T, void> {
void operator()( const T& source, T& dest ) {
dest = source;
}
};
int main() {
static boost::array<int, 5> arr = { 1, 2, 3, 4, 5 };
std::for_each( arr.begin(), arr.end(),
boost::bind( assign<int>(), boost::bind( std::plus<int>(), _1, 3 ), _1 ) );
return 0;
}
std::transform() does allow the output iterator to point to the same element as the beginning of your input range. See here. The code example shows essentially this in the line demonstrating a transform with two input ranges. The output iterator is the same as the first input iterator. Does that make it more palatable?