# How to write a function to match RVO See the Following code. 1. `get1` * ``` Output: 100 ---- 0 ``` * We only call one constructor in this function. 2. `get2` * ``` Output: 100 ---- default move ``` * Call one constructor in A{100}, but call default + move constructor with `named variable`. 3. `get3` * ``` Output: default ---- default ``` * Only call one constructor #### Conclution 1. minimize your return branch. (Like `get3()`) 2. If you have multiple return branch, make the return object be constructor(unnamed) as many as posible. (Like `get1()`) 3. Although you need to do the thing like `get2`, it's ok because it's a move constructor. ```cpp= #include <iostream> struct A { int m; A() { std::cout << "default\n"; } A(int i) : m{i} { std::cout << i << "\n"; } A(const A&) { std::cout << "copy\n"; }; A(A&&) { std::cout << "move\n"; }; }; A get1(int argc) { if (argc > -1) { return A{100}; } return A{0}; } A get2(int argc) { if (argc > -1) { return A{100}; } A a; return a; } A get3(int argc) { A a; if (argc > -1) { a.m=100; } a.m=0; return a; } int main(int argc, char** argv) { auto a = get1(argc); std::cout << "----\n"; auto b = get1(argc - 100); std::cout << "-------\n"; auto c = get2(argc); std::cout << "----\n"; auto d = get2(argc - 100); std::cout << "-------\n"; auto e= get3(argc); std::cout << "----\n"; auto f = get3(argc - 100); std::cout << "----\n"; return 0; } ```