Jump to content
  • Advertisement
Sign in to follow this  
noatom

Why can I call a nonconst function with a const object?

This topic is 1871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The code:

 

class ra{
public:

};


ra hey(){
ra io;
return io;
}

void e(ra& i){
	std::cout << "on" << std::endl;
}
int main() {
	e(hey());
}

 

 

Why does that compile? shouldn't the call to ra in main,return a temporary object,which is a const,an rvalue? So e shouldn't accept it,because it's a const!

 

Why does the above code work?

Edited by noatom

Share this post


Link to post
Share on other sites
Advertisement

Nothing that you don't explicitely declare as const is const. ra() will also construct a new "ra" class object on the stack, did you mean to call hey()? Would still work, though. You might even want to name test functions in a more clear manner, like "foo" for classes, somehow the "ra" naming confused me a bit.

 

Does this solve your question or is there anything else you'd like to know?

 

To show you the difference in code:

 

class foo {};

foo tempCopy()
{
return foo(); //returns a copy of the here created foo-object
}

const foo constCopy
{
return foo(); //returns a const copy of the here created foo
}

void testConstness(foo& object)
{
}

int main()
{
testConstness(tempCopy()); //compiles, but storage of reference/pointer will be invalid as soon as the main() function exits -> imagine this in other //function
testConstness(constCopy()); //won't compile

foo nonconst;
testConstness(nonconst); //compiles
const foo isconst;
testConstness(isconst); //won't compile
}

EDIT: Fixed the paranthesis...

Edited by The King2

Share this post


Link to post
Share on other sites
The code you posted doesn't work/compile. Why do you say it does? Are you using an old, crappy compiler with poor standards compliance?

Also, please include all #include directives to make it easier.

@The King2: his question is about binding a non constant reference to a temporary, which is a bit different from what you are showing/talking about.

Share this post


Link to post
Share on other sites
Strictly, it's not legal C++ but from experience MSVC accepts it. gcc will not though, at least when set to be pedantic.

Share this post


Link to post
Share on other sites

i'm using vs 2010 express,and i can run the code without any problem...

 

sorry,i wanted to call hey() as an argument,anyway,the code is the correct one now,still,it shouldn't work,but it does...

Edited by noatom

Share this post


Link to post
Share on other sites
Alright, now that I'm on a computer instead of a phone, I can properly respond to The King2's post...


// ...
int main()
{
    testConstness(tempCopy); //compiles, but storage of reference/pointer will be invalid as soon as the main() function exits -> imagine this in other //function


Nope, that won't compile. For two reasons. One, you're not calling the function, you're passing the address of the function. And two, even if you did call the function, it would be binding a non-const reference to a temporary, which is illegal.


    testConstness(constCopy); //won't compile


No, it won't, but that's because the constCopy function isn't declared right (it's missing parentheses at the end), and also because you're not calling it here (again, you're passing the address of the function). Then, of course, you have the whole "binding a non-const reference to a temporary" issue. But yes, you are right in that you can't use a non-const reference to refer to a const object.


    foo nonconst;
    testConstness(nonconst); //compiles
    const foo isconst;
    testConstness(isconst); //won't compile
}


These two are actually right. Edited by Cornstalks

Share this post


Link to post
Share on other sites

i'm using vs 2010 express,and i can run the code without any problem...

Ah, that's why. VS 2010 has a non-standard extension to allow this. If you bump up your warning level to level 4, you'll see:

 

warning C4239: nonstandard extension used : 'argument' : conversion from 'ra' to 'ra &'

          A non-const reference may only be bound to an lvalue
 
If you go into your project properties, then C/C++, then Language, you can see an option to "Disable Language Extensions" and if you disable them, your code will no longer compile.
 
GCC, VC++, and other compilers often include some "extensions" to the standard language.
Edited by Cornstalks

Share this post


Link to post
Share on other sites

Nope, that won't compile. For two reasons. One, you're not calling the function, you're passing the address of the function. And two, even if you did call the function, it would be binding a non-const reference to a temporary, which is illegal.

 

Ouch, I missed the () behind the functions, thats what you get from typing rushed out code from your mobile phone, yikes. But as somebody already pointed out, "two" is allowed in VS Studio. Is there some implication other than if we decided to store the reference why we should not do that?

 

sorry,i wanted to call hey() as an argument,anyway,the code is the correct one now,still,it shouldn't work,but it does...

 

It has already been elaborated why it works, at least for visual studio. Could you further explain why you, aside from that, think it should not work?

Share this post


Link to post
Share on other sites

Because temporaries returned from functions are r-values and considered const. You are not supposed/allowed to modify a temporary variable.

 

You are also wrong about the lifetime. The lifetime of a temporary doesn't extend to the end of the surrounding block. It ends with the expression it is used in. One big and good to know exception is having a const reference to it, which will keep the temporary alive for as long as the reference exists.

 

doSomething(createTemporary());
//temporary dies here
 
{
     const Temp& temp = createTemporary();
     //do stuff
}
//temporary dies here, because reference goes out of scope
Edited by Trienco

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!