Passing a temporary object to a reference parameter

M

Martin Rennix

I have a function declared as:

void foo(T&);

And I'm trying to pass a Bar object by doing this:

foo(T());

My compiler (VC++ 2005) gives me a warning "nonstandard extension
used: conversion from T to T&.
It compiles successfully anayway, but I assume will not compile on
other compilers?

If I do:

T bar;
foo(bar);

it compiles without the warning.

So...my question is do I need to define named variables to pass to
reference parameters in order to remain within the standard?

Martin
 
R

Rolf Magnus

Martin said:
I have a function declared as:

void foo(T&);

And I'm trying to pass a Bar object by doing this:

foo(T());

My compiler (VC++ 2005) gives me a warning "nonstandard extension
used: conversion from T to T&.
It compiles successfully anayway, but I assume will not compile on
other compilers?

That's right.
If I do:

T bar;
foo(bar);

it compiles without the warning.

So...my question is do I need to define named variables to pass to
reference parameters in order to remain within the standard?

For references to non-const, yes.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

That's right.


For references to non-const, yes.

Which means that if you change the function to void foo(const T&) it
will work even when you do foo(T());.
 
M

Martin Rennix

Which means that if you change the function to void foo(const T&) it
will work even when you do foo(T());.

Thanks - that did the trick. I always forget to put in the const
qualifier. Java background you see ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top