Sign in to follow this  
EODCyismARDEM

ref arg initialized from constructor

Recommended Posts

EODCyismARDEM    110
class A
{
 public:
  A(int x);
  //...
};

void myfunc(A & a) {
  //...
}

int main()
{
  myfunc(A(2));
  return 0;
} 
myfunc operates on a reference... but no variable inside int main actually holds the object that was initialized with A(2). So why isn't this a compiler error? Is it equivalent to A a = A(2); myfunc(a); ??

Share this post


Link to post
Share on other sites
Endurion    5411
VS 2003 will complain at this if you set the warning level to 4. I do use this construct often though. As i see it the object stays alive until the function call returns.

But since this will be a warning at level 4 i'd like to ask the gurus out there is this a valid piece of code?

Share this post


Link to post
Share on other sites
Shannon Barber    1681
A(2) creates a temporary instance of class A initialized with 2.

It's valid but not best-practice.

myfunc(const A& a) would say it's ok to take a temporary since we're not going to change it anyway.

As-is though, member funtcions of A could have side-effects on other objects or so it could affect the state of the system.

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Quote:
Original post by Endurion
VS 2003 will complain at this if you set the warning level to 4. I do use this construct often though. As i see it the object stays alive until the function call returns.

But since this will be a warning at level 4 i'd like to ask the gurus out there is this a valid piece of code?


This is valid AFAIK. A mutable, temporary variable is created, modified, and destroyed. The warning is there because more often than not, if you need a non-const reference, you were expecting to perform modifications on a variable and have them reflected in the calling code (otherwise it might as well have been a local variable). If discarding this is truely your intention, you can make this clear:

A temp(2);
myfunc( temp );
//note: temp discarded


If it dosn't make sense for your function to modify the temporary, then myfunc should accept a reference-to-const :

void myfunc(const A & a)[


Either of these should eliminate the warning.

Share this post


Link to post
Share on other sites
EODCyismARDEM    110
That answers that - thanks!

BTW: I have been using reference to const ... sorry I forgot to put that in my original code listing. For g++, it gives an error if it is not const.

Share this post


Link to post
Share on other sites
Nitage    1107
Quote:
Original post by Shannon Barber
A(2) creates a temporary instance of class A initialized with 2.

It's valid but not best-practice.

myfunc(const A& a) would say it's ok to take a temporary since we're not going to change it anyway.

As-is though, member funtcions of A could have side-effects on other objects or so it could affect the state of the system.


It's not valid. It's a non-standard Microsoft extension.

Passing a non-const reference to a temporary object is illegal.

In VS2005 with warning level 4:
warning C4239: nonstandard extension used : 'argument' : conversion from 'A' to 'A &'


With non-standard extensions turned off (/Za):
error C2664: 'myfunc' : cannot convert parameter 1 from 'A' to 'A &'


edit: Also note that the MS compilers only allow this for user defined types.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this