4

According to Wikipedia, function calls don't copy parameters which are are references to a const type:

void f_slow(BigObject x);
void f_fast(const BigObject& x);

f_slow(y); // slow, copies y to parameter x
f_fast(y); // fast, gives direct read-only access to y

Why does the reference need to be const? Wouldn't a non-const reference accomplish the same:

void f_should_be_fast(BigObject& x);
Natan Yellin
  • 6,063
  • 5
  • 38
  • 57
  • 1
    I would get a book rather than following wikipedia. It is written be a lot of well intentioned but often inexperienced developers. Because every beginner in C++ thinks he/she knows the language after a month. Just reading the first sentence makes me cringe. – Martin York Aug 16 '11 at 15:39
  • possible duplicate of [How come a non-const reference cannot bind to a temporary object?](http://stackoverflow.com/questions/1565600/how-come-a-non-const-reference-cannot-bind-to-a-temporary-object) – Bo Persson Aug 16 '11 at 15:40

7 Answers7

9

Yes, any kind of reference will do. Making it const makes it more flexible as it can accept const variables or temporaries as parameters, and documents (and enforces) your intent not to modify the parameter.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 2
    You can't always trust Wikipedia! – Raedwald Aug 16 '11 at 14:58
  • 1
    @Raedwald, the quoted Wikipedia content isn't wrong, just misleading. And it might be taken out of context, I haven't followed the link. – Mark Ransom Aug 16 '11 at 15:00
  • 1
    I suspected as much, but I wanted confirmation before I fixed the Wikipedia page. The same mistake appears on http://en.wikipedia.org/wiki/String_(C%2B%2B) – Natan Yellin Aug 16 '11 at 15:06
  • @Natan: it's not an error, although it could be clearer. It *is* best to use a const reference for the purpose they're talking about, i.e. replacing a parameter passed by value. The reason is as stated on the Wikipedia page, that if the current code modifies its copy then it's best to find out about it immediately since the const-reference version of the function will fail to compile. A non-const reference version would compile but modify the caller's object, which could easily break something. – Steve Jessop Aug 16 '11 at 15:13
  • 1
    @Steve, Wikipedia *is* wrong, because it implies that `const` is necessary to avoid the overhead of copying parameters. From the article: *References qualified with const are a useful way of passing large objects between functions that avoids this overhead* – Natan Yellin Aug 16 '11 at 15:18
  • @Natan: the text you quote does *not* imply what you say it implies. It states that references qualified with const are (a) useful and (b) overhead-avoiding. It does not state which (if either) of those properties references to non-const have. I agree with you to the extent that it could be clearer which part of "non-const reference" is responsible for each of those properties - `const` is about "useful", in particular not breaking existing code both in the way Wikipedia mentions and the way Mark and Mike mention. – Steve Jessop Aug 16 '11 at 15:22
  • @Natan: `const` is neccessary because without you cannot pass temporary objects as arguments. –  Aug 16 '11 at 15:22
  • @Steve, I know I've used non-const references to a temporary parameter in Microsoft Visual C++ in the past, and there was a warning dedicated to that condition. I'm searching the documentation and it looks like Microsoft back-pedaled on that extension, so I'm removing my comment. – Mark Ransom Aug 16 '11 at 15:42
8

The const doesn't affect the speed, really. It just prevents the function from modifying the original object, making it similar to pass-by-value semantics (but not identical, obviously).

Note that it may also be worth reading this: Want Speed? Pass by Value

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

With a reference to non-const you can't pass a const object (or a reference to const object) into the function and this will lead to worse const correctness. It won't affect execution speed however.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
1

The const keyword signals that the parameter value will not be changed in the function/method. This might result in a better performance, because the compiler can adjust the code with some performance improvements, though this is not granted.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • 1
    `const`ness can be `const_cast`ed away, so I don't think the compiler can perform any optimizations based on this sole information. See [Constants and compiler optimization in C++](http://stackoverflow.com/q/212237/20984). – Luc Touraille Aug 16 '11 at 15:41
0

Yes it would accomplish the same, but the const is a signal to the compiler that can make optimizations if an object is never going to be modified.

Blazes
  • 4,721
  • 2
  • 22
  • 29
  • There aren't really any optimizations that can be made due to a reference-to-const, since that doesn't signal to the compiler that the object is never modified. Sometimes an object can validly be modified through an alias, despite there existing a reference-to-const that refers to it. If the compiler can rule out modification through an alias, then at least in theory it can do so regardless of whether the reference is const. A const *object* can enable optimizations, because that's rather stronger than just a reference-to-const. – Steve Jessop Aug 16 '11 at 15:27
  • For more about `const` and optimization, you can have a look at [Constants and compiler optimization in C++](http://stackoverflow.com/q/212237/20984). – Luc Touraille Aug 16 '11 at 15:38
0

Reference in this case is just non-modifiable because of qualifying it with const. By not qualifying it const, it is just a modifiable reference. The only concern is the whether the reference argument is modifiable or not. Speed has nothing to do with const qualification, AFAIK.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
0

Using a const reference provides you Const correctness.
In a nutshell:

It means that the type to which the reference refers cannot be modified inside the function & at the same time you do not incur the overhead of pass by value.

It helps honest programmers from making honest mistakes & provides the compiler to perform any optimization(it at all) in can perform any.

Alok Save
  • 202,538
  • 53
  • 430
  • 533