Jump to content

  • Log In with Google      Sign In   
  • Create Account


const problems


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 noatom   Members   -  Reputation: 759

Like
0Likes
Like

Posted 20 March 2013 - 03:08 PM

class x{
int i;
public:
x(int ii=0);
void modify();
};

X f5(){
return X();
}

void f7(X& x){
x.modify();
}

int main(){
f7(f5()); <-----COMPILE ERROR THERE

}

 

Can someone please explain why does that error appear ?


Alexander Turda - The place where I talk about games, coding, movies, and whatever passes through my mind.


Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5093

Like
1Likes
Like

Posted 20 March 2013 - 03:12 PM

It doesn't say const anywhere in the code!

 

Presumably it is moaning that you are passing an unnamed temporary value (i.e. return value from f5()) as a reference parameter which isn't legal (but may be allowed with a warning) since you don't want to be modifying an unnamed temporary value by accident.

 

Try doing

 

X myX = f5();

 

f7(myX);

 

instead, that should work...

 

EDIT: And we aren't psychic (try the math & psychics forum instead for that) please post the full error message and compiler you are using next time kthx ;)


Edited by Paradigm Shifter, 20 March 2013 - 03:17 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 SiCrane   Moderators   -  Reputation: 9337

Like
2Likes
Like

Posted 20 March 2013 - 03:18 PM

The return from a function is an rvalue, so cannot be bound to a non-const lvalue reference like X & (without a vendor extension like one that MSVC has) but can be bound to a const lvalue reference. With a C++11 compiler you can also bind the return value to a function to an rvalue reference like X &&.

#4 frob   Moderators   -  Reputation: 18371

Like
2Likes
Like

Posted 20 March 2013 - 05:57 PM

In simpler terms...

Every expression in c++ is either an lvalue or an rvalue. The names come from which side of the assignment operator they can exist on.

An lvalue is (basically) any named object. All variables are lvalues. An lvalue is something that can exist for more than a single statement.

An rvalue is a temporary value that does not necessarily exist for more than a single statement. The result of a function, as in you example, is an rvalue. The value exists only for a single expression then is lost.

Compilers are very aggressive at optimizing rvalues out of existence; often they only exist in a register or can be sometimes eliminated completely.

You cannot take an address of an rvalue. It is temporary and may not even really exist.

You need to create an lvalue, and that lvalue can be passed by reference to the second function.

Alternatively you can pass it as a const reference. The compiler is allowed to (essentially) create a temporary constant object and pass that object by reference.


Or written differently: because it is passed by non-const reference you must create a variable, assign the first function's result to the variable, and then pass the variable to the second function. Or you will need to modify your function to accept a const reference; do this if you can guarantee you will never modify the input.
Check out my personal indie blog at bryanwagstaff.com.

#5 Paradigm Shifter   Crossbones+   -  Reputation: 5093

Like
0Likes
Like

Posted 20 March 2013 - 06:28 PM

I think I won the "in simpler terms" game!

 

I was going to suggest using a const reference for function f7 but it calls a function called modify() on the parameter passed, I guessed that might not be a good suggestion, so I made a poor joke instead ;)


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#6 AllEightUp   Moderators   -  Reputation: 4066

Like
1Likes
Like

Posted 20 March 2013 - 06:51 PM

class x{
int i;
public:
x(int ii=0);
void modify();
};

X f5(){
return X();
}

void f7(X& x){
x.modify();
}

int main(){
f7(f5()); <-----COMPILE ERROR THERE

}

 

Can someone please explain why does that error appear ?

 

Beyond the very descriptive and detailed descriptions, this could be a simple case of compiler differences biting you on the ass.  The given code is valid for Visual Studio and unless you set warning level to 4, it won't even bitch about this.  The very simple answer is that:

 

f7( f5() );

 

is actually illegal C++ yet VC cheats and allows it via a syntactic sugar addition.  GCC on the other hand will pitch a fit and give you an error.  The "fix" is simple:

 

temp = f5();

f7( temp );

 

So, if you have been using GCC and reading books with a VC source (pretty common unfortunately), you need to use the more explicit syntax, VC does this behind your back and hides it unless you set warnings to 4.  (Not even sure VC warns about this at w4, think so though.)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS