4

This is my first post and I hope I'm doing it ok. I have tried to solve this problem for a while now but just went for an intermediary variable in the meantime. Well this is what I mean:

//from a pre-built library
double getValue(int idx)
{ 
  //Returns some value from within a class 
}

//from a function I created
void setValue(double &input)
{
  //set some value here
}

I am currently doing my program as follow:

double numberOne;
numberOne = getValue(0);
setValue(numberOne);

This works and compiles. I would like to however do something as follows:

setValue(getValue(0));

However I can't seem to get it right (have tried a number of referencing\de-referencing things but I'm just shooting in the dark). I would like to know if it's even possible to do so? Furthermore if it is possible to do so, is there any speed\memory space advantages to perform it this way rather than having an intermediary storage value (aka numberOne). It really isn't a concern for a type double value but when its a class with a much larger footprint then I would like to reduce the amount of memory usage\deep copying as much as possible for speed\memory considerations.

On a side note are there any books or online resources that can help me to speed up my C++ programs with other efficiency improvements to speed\memory usage.

Thanks so much in advance for any help you may be able to provide.

1
  • If setValue potentially changes its input, how are you going to notice that change if you pass something that you cannot later query? There is a reason references to non-const don't bind to rvalues. Commented Aug 19, 2011 at 8:48

3 Answers 3

4

Change this :

void setValue(double &input)

to

void setValue(double input) 

After all, input should truely be input, and sosetValue() should treat this as such. Here performance is not even worse if you pass by value. However, for big classes, you might want to change it to reference to avoid copying and to achieve better performance:

void setValue(const VeryBigClass & input) 
Sign up to request clarification or add additional context in comments.

3 Comments

The problem is that setValue is allowed to write to the variable passed. And how is it to write to a return value of some other function? &input must reference an actual memory location, not an intermediate value floating around some registers.
Doesn't this make a copy of whatever input is though? Thanks for the quick reply. :)
@SF. The standard simply says that a temporary cannot be bound to a reference but only to a reference to const. That it is actually possible to implement this shows one of the evil VS extensions. Nothing about registers or memory here.
2

The return value from the function is a temporary. There is a rule that you cannot bind a temporary to a non-const reference, because it turned out to cause a lot of errors.

You can bind the temporary to a const reference though, like

SetValue(const double& value);

This can be good for larger or more complex types, but for primitive types like double you can just as well pass it by value

SetValue(double value);

7 Comments

I'd really like to know which errors it caused. Stroustrup ruled it out from the very early days of C++ with anecdotical evidence, however VC++ has long allowed it as an extension and I heard no-one complaining about it (at least, complaining that it created error, when people realize it's non-portable, they usually complain about the other compilers). AFAIK, it would be no worse that const& binding, and I have seen crashes because of this one.
I had a feeling it was something along those lines. Thanks so much for the help. I am going to use the const because my variable is a much more complex type (opencv's Mat type actually) so I'm going to try this.
@Matthieu - Bjarne's example is that sometimes the parameter is converted and you get a reference to a temporary. When expecting to update the parameter they were very surprised that the update was lost. void update(double&); int i = 1; update(i); would seem to "work" but not update anything.
@Matthieu M. I agree with respect to const& binding. I believe that the only reason that is allowed is that not allowing the const& binding would mean that with functions like printout( type const & ) you would have to duplicate or add an extra overload printout( type ) that calls the previous one in case of temporary arguments, the implementation of which is just the wording for the rvalue to const & binding item in the standard. As to why binding to a non-const reference causes less problems overall than binding to non-const, probably because most people just don't do it.
@Bo Persson: Honestly, I am more concerned about the fact that it does work with const&. Integer Promotion for binding to const& is really weird I find. But then, I never really appreciated Integer Promotion.
|
0

You transform your setvalue :

void setValue(const double &input)
{
  //set some value here
}

That should remove the problem.

If your "double" is big enough (here it is useless), there are advantages getting a reference. This way you will only copy the part of the object that interest you in the setvalue().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.