开发者

How to pass "literal" integers by reference in C++

开发者 https://www.devze.com 2023-04-11 22:10 出处:网络
To avoid the inefficiency of copy-by-value when calling a function (say, "fillRect"), I want to pass the parameters by reference.

To avoid the inefficiency of copy-by-value when calling a function (say, "fillRect"), I want to pass the parameters by reference.

If I supply the parameters as declared local variables, it works fine. But if I supply any as "literal" integers, I get a compile error (no matching function).

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

int x=10, y=20, w=100, h=80;

fillRect(x, y, w, h); // this compiles and works!
fillRect(x, y, 100, 80); // but this doesn't compile ... why?

What gives?


(Forgive my naivety: I'm pretty new to C++.)

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 thank you all! Even so, my question was really more about why literal values can't seem be passed by refe开发者_如何学Pythonrence, which has been addressed by the accepted answer.


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).


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号