I have read Dave Abrahams article on RVO and a few other Q/As on SO (14043609, 9293726 and 10818278) but I still have a question. When I compile and run the following code, I get this output:
Address of v in func 0x7fffac6df620
Address of v.data in func 0x2081010
Address of v in main 0x7fffac6df690
Address of v.data in func 0x20811b0
9
To me it seems that a copy is made. How do I pass large objects out of functions? Please note that I want to return one or more objects without writing an explicit structure for it. I used GCC 4.6.3 with -O2. Edit: The first two answers showed me that I expected too much from the compiler. I added a main2 that behaves in the same way, e.g. the printed addresses are different. I would like to emphasize that the motivation is efficient return of large objects.
#include <iostream>
#include <vector>
#include <tuple>
std::tuple<std::vector<int>, double> func() {
std::vector<int> v;
v.reserve(100);
for (int k=0;k!=100;k+=1)
v.push_back(k);
double a = 5.0;
std::cout << "Address of v in func\t" << &v << std::endl;
std::cout << "Address of v.data in func\t" << v.data() << std::endl;
return make_tuple(v, a);
}
int main() {
std::vector<int> v;
double a;
std::tie(v, a) = func();
std::cout << "Address of v in main\t" << &v << std::endl;
std::cout << "Address of v.data in func\t" << v.data() << std::endl;
std::cout << v[9] << std::endl;
return 0;
}
int main2() {
auto tp = func();
std::vector<int> & v = std::get<0>(tp);
double & a = std::get<1>(tp);
std::cout << "Address of v in main\t" << &v << std::endl;
std::cout << "Address of v.data in func\t" << v.data() << std::endl;
std::cout << v[9] << std::endl;
return 0;
}