Rule of thumb for when passing by value is faster than passing by const reference?

前端 未结 7 1823

Suppose I have a function that takes an argument of type T. It does not mutate it, so I have the choice of passing it by const reference const T&

7条回答
  •  别那么骄傲
    2020-12-15 16:34

    Passing a value instead of a const reference has the advantage that the compiler knows the value isn't going to change. "const int& x" doesn't mean the value cannot change; it only means that your code is not allowed to change it by using the identifier x (without some cast that the compiler would notice). Take this awful but perfectly legal example:

    static int someValue;
    
    void g (int i)
    {
        --someValue;
    }
    
    void f (const int& x)
    {
        for (int i = 0; i < x; ++i)
            g (i);
    }
    
    int main (void)
    {
        someValue = 100;
        f (someValue);
        return 0;
    }
    

    Inside function f, x isn't actually constant! It changes every time that g (i) is called, so the loop only runs from 0 to 49! And since the compiler generally doesn't know whether you wrote awful code like this, it must assume that x might change when g is called. As a result, you can expect the code to be slower than if you had used "int x".

    The same is obviously true for many objects as well that might be passed by reference. For example, if you pass an object by const&, and the object has a member that is int or unsigned int, then any assignment using a char*, int*, or unsigned int* might change that member, unless the compiler can prove otherwise. Passed by value, the proof is much easier for the compiler.

提交回复
热议问题