return pre sized vector

Hi guys..
I stumble a upon a little which i cannot come around..

I have function of type pair, which returns a 2 coordinates consisting of displacement on the x-axis and displacement on the y-axis.

The problems is this output is an input to another system, the other system does not use efficiency of pairs, it uses an input vector. Therefore i use a temp vector which i feed the pair.first and pair.second, which creates an redundant extra call, making the program slower. Would it be possible create a sized vector.. to overcome problems such a timing and so on.. and feed that directly to the other system.. creating a nearly perfect solution?
So your code is roughly is:
1
2
std::pair<int, int> your_code();
void other_code(std::vector<int>);
and you are doing something like:
1
2
auto foo = your_code();
other_code({foo.first, foo.second});


In this case it is as efficient as you can get without changing either return value of your funtion or argument list of other function: teplorary pair foo will be optimised away, and vector will be constructed in place without creating unnecessary temporary.
Sadly I do not know any optimisation which would use something like small string optimization for vectors, so you still probably have one heap allocation here.
Topic archived. No new replies allowed.