ref arg initialized from constructor

Started by
5 comments, last by Nitage 17 years, 11 months ago

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); ??
~EODCyismARDEM
Advertisement
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?

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

I'm using g++ 4.0.1 with -Wall and it doesn't give a peep.
~EODCyismARDEM
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.

- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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.
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.
~EODCyismARDEM
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.

This topic is closed to new replies.

Advertisement