Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Type conversion of reference to a temporary object

Reply
Thread Tools

Type conversion of reference to a temporary object

 
 
Stefan Strasser
Guest
Posts: n/a
 
      03-28-2005
JH Trauntvein schrieb:

> void foo(std::istream &in)
> {
> int i, j;
> in >> i >> j;
> } // foo
>
>
> int main()
> {
> foo(std::istringstream("1 1"));
> return 0;
> } // main
>
>
> g++ gives the following complaint when I attempt to compile this code:
>
> error: invalid initialization of non-const reference of type
> 'std::istream &' from a temporary of type 'std::istringstream'
>
> I was under the impression that my code above was standard-conforming.
> Am I wrong on this point?


calling a constructor creates a temporary. temporaries are const. your
parameter of "foo" is not. you could pass it by value to make it work e.g.
since vs .net seems to be very standard conforming I guess this is some
sort of extension which can be turned off.
 
Reply With Quote
 
 
 
 
JH Trauntvein
Guest
Posts: n/a
 
      03-28-2005
I have something like the following code that I am trying to convert
from vs studio .net to g++ under linux:

#include <sstream>
#include <iostream>


void foo(std::istream &in)
{
int i, j;
in >> i >> j;
} // foo


int main()
{
foo(std::istringstream("1 1"));
return 0;
} // main


g++ gives the following complaint when I attempt to compile this code:

error: invalid initialization of non-const reference of type
'std::istream &' from a temporary of type 'std::istringstream'

I was under the impression that my code above was standard-conforming.
Am I wrong on this point?

Regards,

Jon Trauntvein

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-28-2005
JH Trauntvein wrote:
> I have something like the following code that I am trying to convert
> from vs studio .net to g++ under linux:
>
> #include <sstream>
> #include <iostream>
>
>
> void foo(std::istream &in)
> {
> int i, j;
> in >> i >> j;
> } // foo
>
>
> int main()
> {
> foo(std::istringstream("1 1"));
> return 0;
> } // main
>
>
> g++ gives the following complaint when I attempt to compile this code:
>
> error: invalid initialization of non-const reference of type
> 'std::istream &' from a temporary of type 'std::istringstream'
>
> I was under the impression that my code above was standard-conforming.
> Am I wrong on this point?


Yes. You create a temporary of type 'std::istringstream' in the main
function, and references to non-const cannot be bound to a temporary.
VC++ allows that as an extension. You will have to do

std::istringstream is("1 1");
foo(is);

V
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
Passing a temporary object to a reference parameter Martin Rennix C++ 3 05-07-2007 04:33 AM
reference to the temporary object Raider C++ 3 06-22-2006 03:34 PM
Const reference to a temporary object - Why? Dave Rahardja C++ 15 11-03-2005 03:45 AM
A reference to non-const to be bound to a temporary object John Ky C++ 9 02-23-2004 12:53 AM



Advertisments