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

Started by
14 comments, last by Khatharr 11 years ago

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?

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...

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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Strictly, it's not legal C++ but from experience MSVC accepts it. gcc will not though, at least when set to be pedantic.

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...

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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

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?

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
f@dzhttp://festini.device-zero.de

*snip*

+1, I'm going to leave this link here for the curious

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement