quiz: add 0 to return value: gotcha # lval

Given calcTax(), I sometimes convert the returned value — simply add 0. I used to think this is completely harmless and has no effect on the program.

Now I don’t feel that way. calcTax() could return by reference, so we can take the address of this “expression” like

&calcTax()

In other words, calcTax() is an lvalue expression. If you add 0, you get a rvalue expression! Basically a temporary object. You can’t take its address.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s