Image

Imageutil wrote in Imagecpp

How to "return" a vector

Say you have code that generates a sequence of doubles. Which is better? Having the code create a local vector and return it (incurring a copy), having the local code dynamically allocate a vector and return it, or requiring the user to pass in a vector to be filled in. Ie, vector<double>foo() vs. vector<double>* foo() vs. void foo(vector<double>& result).

My preference is for vector<double> foo(). This feels cleaner to me. There's no need to worry about freeing the returned memory nor do you have to deal with dereferencing a pointer. Somewhat petty things I know.

What's your preference?

Maybe the best thing to do would be to follow the STL:
template<typename T>
T foo(T first, T last)

But I think this might turn off some of the other users of my code since they have not seen much of this.