I have a function which returns a vector or set:
set foo() {
set bar;
// create and massage bar
return bar;
}
set
Modem C++ compiler will implement: given a type T
:
I usually work around this by having function signature as
void foo(set<int> *x)
Just pass it by reference or the other option is already mentioned in the comment.
Edit: I have changed the argument type to illustrate that x could be changed.
set<int> s;
foo(&s);
This is only preferred when you have an old compiler. I suppose that could be the case with some of the projects.
And, better thing to do will be Either to use move semantics with c++11. Or go ahead returning the container and look into RVO in modern compilers.
Check out return value optimization. A modern compiler will optimize this situation, and in straightforward situations like these, no copy will be made on any of the major compilers.
In principle, you could also create your object outside the function, and then call the function and pass the object to it by reference. That would be the old way of avoiding a copy, but it is unnecessary and undesirable now.