vector<obj> func(const vector<obj>& input);
All compilers implement RVO either by default or when you turn optimizations on, if you don't turn optimizations on you don't care about performance, so that would not be an issue anyway.
If your compiler is C++11 conforming, then in the worst case instead of copying 1 pointer you will pay for 3 pointers when move semantics kick in, which compared with the cost of allocating the memory is really no cost.
Build a simple meaningfull interface, then measure the performance and bottlenecks and optimize from there.
Depending on your use pattern, if you can reuse the output container, you might want to transform the above into:
void func(vector<obj>& output, const vector<obj>& input);
This will have the same performance when creating a new vector, but if you call this function in a loop, you may be able to avoid a few allocations:
std::vector<obj> output;
for ( ... ) {
output.clear();
func(output, input);
// compare with
for ( ... ) {
std::vector<obj> output = func(input);
In the first block of code, if all of the vectors are of the same size, the clear()
will remove the elements but leave the buffer intact, so that the next call to func
need not allocate. In the second case it needs to allocate inside func
and will deallocate at the end of the scope.
This is a slightly uglier interface to use, so I would opt for the first one (which semantically is cleaner), and use the second only if the first proves to have an issue with multiple allocations.