473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Type conversion of reference to a temporary object

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

Jul 23 '05 #1
2 1488
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
Jul 23 '05 #2
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.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Whitney Kew | last post by:
Hello, I have a question regarding type conversion operators. Let's say I have a simple (nonsensical) class as follows: class MakeNewInt { private: int* _n;
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
1
by: tsar.omen | last post by:
Hello everyone, during my coding practice I have encounted the following problem: Vital code pieces: --- template <class T> class SmartPtr {
6
by: Dhirendra Singh | last post by:
Hi, The following C++ program is not compiling on my system. #include <iostream> using namespace std; class complex { double re, im; public: complex( ) :re(0), im(0) {}
11
by: tthunder | last post by:
Hi @all, My small example does not compile... I know, that this (as always) has reasons, but I want to know WHY? BTW: I only get errors with g++ (4.x), BCB (6.0),... VS C++ (2005) works...
5
by: Ivan Novick | last post by:
Is the string below automatic storage duration? #include <iostream> #include <string> int main(int argc, char** argv) { std::cout << std::string().size() << std::endl; return 0; }
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
22
by: Ruben | last post by:
Why would a method that is defined to return a reference such as with the operator overload of , operator, href& operator(int index){ return _array; } not cause a type mismatch compiler...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.