How to pass “literal” integers by reference in C++ (Newbie)

后端 未结 5 455
温柔的废话
温柔的废话 2020-12-03 17:23

Edit: As many people have pointed out, pass-by-reference isn\'t generally appropriate as an optimisation for primitive types. This is excellent to know, so

相关标签:
5条回答
  • 2020-12-03 17:33

    First you should take heed of the other's advice to prefer passing by value for simple types. C (and by extension C++) were designed with this use case in mind, and that's what they're optimized for.

    Second you should always use a const reference unless you intend to modify the variable. A literal can be bound to a const reference but not a non-const one.

    0 讨论(0)
  • 2020-12-03 17:35

    You seem to already be familiar with the problems of copy-by-value, so you've eliminated copy-by-value. And now you're confused that passing raw values fails?

    Now I'm lost.

    0 讨论(0)
  • 2020-12-03 17:43

    You cannot bind a literal to an lvalue reference to non-const (because modifying the value of a literal is not an operation that makes sense). You can however bind a literal to a reference to const.

    So this will compile if you declare fillRect as:

    void fillRect(int const& x, int const& y, int const& width, int const& height)
    

    In this case you are passing ints. ints are so cheap to copy that passing by them by reference will probably make the performance of your program worse.

    The function fillRect is probably so expensive that the cost of passing its arguments is totally irrelevant in any case. Or maybe it will be inlined, and there will be no cost whatsoever to passing the arguments. These sorts of micro-optimisations are usually not optimisations at all, and should always be guided by the results of profiling (if they are done at all).

    0 讨论(0)
  • 2020-12-03 17:50

    To avoid the inefficiency of copy-by-value when calling a function

    Stop right there.

    Passing by reference does not necessarily mean "fast". This goes doubly so for basic types. Indeed, accessing basic types by reference will be slower than doing so by value. A reference is not magic. It's generally implemented as a pointer. So every time you access that value, you are likely doing a pointer dereference.

    This sounds like some kind of micro-optimization. Do not optimize without profiling. Unless you have really good reason to expect the performance of your application to hinge on value vs. reference parameters, just do what makes sense.

    You should only pass basic types by reference if you intend to modify them.

    0 讨论(0)
  • 2020-12-03 17:51

    Passing by reference is actually slower for such small values. To pass by reference, it is, under-the-hood, passing a pointer (which is an int-sized value anyway). Then, there is a hidden extra pointer indirection that is not free. It is more direct to simply pass the value.

    Do this:

    void fillRect( int x, int y, int width, int height )
    {
        // do something
    }
    

    The compiler will most likely inline your function anyway, unless its big. So you wouldn't have been able to improve the performance by being "clever" in how you declared the function.

    0 讨论(0)
提交回复
热议问题