Is call by reference safe in this C++ example?
<p>Comming from Java I would like to implement the following "test multiple implementations" idiom in C++</p>

<pre><code>void testMethod() {
    List veryBigList = createNewRandomList(10000);
    Algo algo = createAlgo(veryBigList);
    algo.run();
}

// this can be overwritten from a subclass to test against different algo
Algo createAlgo(List list) {
    Algo a(list);
    return a;
}

List createNewRandomList(int size) {
    List l = new ArrayList(size);
    // fill list with random objects
    return l;
}
</code></pre>

<p>I'm a bit lost how to do this the right way in C++ ... when I'm doing it this way:</p>

<pre><code>void test_method() {
    vector&lt;string&gt; big_list(10000);
    init_random_list(big_list);
    algo my_algo = create_algo(big_list);
    my_algo.run();
}

algo create_algo(vector&lt;string&gt; list) {
    algo a(list);
    return a;
}

void init_random_list(vector&lt;string&gt; list) {
    // fill list with random objects        
}
</code></pre>

<p>then what will C++ do? Will it copy the full list to go to create_algo and then on return it'll copy the algo (and the list again)?? </p>

<p>But when using call by reference I would access the reference in test_method outside of the scope of algo causing a lot trouble and violating the <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization" rel="nofollow">RAII</a> principle, right?</p>

<p>I know for some implementations of C++ they optimize speed for call by value according to <a href="http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/" rel="nofollow">this doc</a>, but can I rely on that for g++ and visual c++?</p>
