Jump to content

Recommended Posts

We talked with @elexis about constant references and I gave an example why passing by value may be better than passing by constant reference. During todays refactoring I met another important things that you need to know about constant references.


Take a look at the following code. Do you see a problem? (It's our code from ps/Shapes.h).

// Interface
class CSize
    // ...
    void operator/=(const float& a);
    // ...
    float cx, cy;

// Implementation
void CSize::operator/=(const float& a)
    cx /= a;
    cy /= a;

If not, would the following usage example help you?

CSize size(2560, 1440);
// Normalize the size.
if (size.cx)
    size /= size.cx;
debug_printf("%f %f", size.cx, size.cy);

You'll get:

1.0 1440.0

Because the a references to cx, and the first line of the operator modifies the cx. And in the next we just divide the cy by 1. It may happen in each case where we get the same class.

I fixed the problem in D1809.


Another important thing is a lifetime. Let's take another look at another example (fictional, because I didn't find more detailed example in our code yet):

std::vector<Node> nodes;
// ...
duplicate(nodes[0], 10);
// ...
void duplicate(const Node& node, size_t times)
    for (size_t i = 0; i < times; ++i)

From first look it seems ok. But, if you know how std::vector works then you know, that on each push_back std::vector can reallocate array to extend it. And then all iterators and raw pointers are invalid, including our constant reference. So after few duplication calls it may contain a trash.

So, you need to be careful with such cases.

  • Like 7
Link to post
Share on other sites
7 hours ago, GunChleoc said:

For the duplicate function, resize and fill from the standard library should do the trick too - of course, with a copy instead of a reference.



Yeah, every function that may invalidate the constant reference is potentially dangerous.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...