473,320 Members | 1,746 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,320 software developers and data experts.

What do you expect this to print?

What should I expect the following code to print? Is it defined in the
Standard? What does it produce for you? I was kind of surprised by what
GCC 4.0.2 made of it.

#include <string>
#include <iostream>

typedef std::string object;

class arrow{
public:
arrow(object& domain_
,object& codomain_
,const std::string& name_)
:_domain(domain_)
,_codomain(codomain_)
,_name(name_)
{
std::cout << _name << std::endl;
}

object domain(){ return _domain; }
object codomain(){ return _codomain; }

operator object () { return _codomain; }

std::ostream& print(std::ostream& out) const {
return out<<_domain<<"--"<<_name<<"->"<<_codomain;
}

private:
object _domain;
object _codomain;
const std::string& _name;
};

std::ostream& operator<<(std::ostream& out, const arrow& a ){
return a.print(out);
}

int main(){
object a("a");
object b("b");
arrow f(a,b,"f");
std::cout << f << std::endl;
}

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Oct 25 '05 #1
5 1485
Steven T. Hatton wrote:
What should I expect the following code to print? Is it defined in the
Standard? What does it produce for you? I was kind of surprised by what
GCC 4.0.2 made of it.

As expected, it outputs:
f
a--->b

However, if you change _name member from const std::string& to
std::string it produces:
f
a--f->b

(Again - as expected).

Oct 25 '05 #2
* Steven T. Hatton:
What should I expect the following code to print? Is it defined in the
Standard?
It's Undefined Behavior.

What does it produce for you? I was kind of surprised by what
GCC 4.0.2 made of it.

#include <string>
#include <iostream>

typedef std::string object;

class arrow{
public:
arrow(object& domain_
,object& codomain_
,const std::string& name_)
:_domain(domain_)
,_codomain(codomain_)
,_name(name_)
Binding a reference to a temporary is only OK as long as that temporary
persists.

{
std::cout << _name << std::endl;
}

object domain(){ return _domain; }
object codomain(){ return _codomain; }

operator object () { return _codomain; }

std::ostream& print(std::ostream& out) const {
return out<<_domain<<"--"<<_name<<"->"<<_codomain;
}

private:
object _domain;
object _codomain;
const std::string& _name;


Remove the 'const' and especially the '&' and you'll be okay.

Possibly you thought that binding a reference to const to a temporary
magically makes that temporary persist.

That's only for a local reference, and it's actually a bit more subtle
(formally a new temporary is created with lifetime extended to the scope of
the reference, and the initializer rvalue is copied to that temporary).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 25 '05 #3
On Tue, 25 Oct 2005 05:32:46 -0400, "Steven T. Hatton"
<ch********@germania.sup> wrote:
What should I expect the following code to print? Is it defined in the
Standard? What does it produce for you? I was kind of surprised by what
GCC 4.0.2 made of it.
(...)class arrow{
public:
arrow(object& domain_
,object& codomain_
,const std::string& name_)
:_domain(domain_)
,_codomain(codomain_)
,_name(name_)
{
std::cout << _name << std::endl;
}
(...)private:
object _domain;
object _codomain;
const std::string& _name;
};
(...)
int main(){
object a("a");
object b("b");
arrow f(a,b,"f");
std::cout << f << std::endl;
}


The result is undefined.

You are initialising a temporary string with "f", then assigning a
reference to it (_name). After f ceration, the temporary disappears
(=is not longer valid), and you have an invalid reference.

Oct 25 '05 #4
Alf P. Steinbach wrote:
* Steven T. Hatton:
What should I expect the following code to print? Is it defined in the
Standard?
It's Undefined Behavior.

What does it produce for you? I was kind of surprised by what
GCC 4.0.2 made of it.

#include <string>
#include <iostream>

typedef std::string object;

class arrow{
public:
arrow(object& domain_
,object& codomain_
,const std::string& name_)
:_domain(domain_)
,_codomain(codomain_)
,_name(name_)


Binding a reference to a temporary is only OK as long as that temporary
persists.


As I suspected.
{
std::cout << _name << std::endl;
}

object domain(){ return _domain; }
object codomain(){ return _codomain; }

operator object () { return _codomain; }

std::ostream& print(std::ostream& out) const {
return out<<_domain<<"--"<<_name<<"->"<<_codomain;
}

private:
object _domain;
object _codomain;
const std::string& _name;
Remove the 'const' and especially the '&' and you'll be okay.


Indeed.
Possibly you thought that binding a reference to const to a temporary
magically makes that temporary persist.
I really hadn't thought about it until I did it. It was just toy code I was
using to try and reason out some obtuse mathematical language.
That's only for a local reference, and it's actually a bit more subtle
(formally a new temporary is created with lifetime extended to the scope
of the reference, and the initializer rvalue is copied to that temporary).


I guess I had intuitively expected the temporary to outlive its scope in
that case. I'm glad I messed it up in that situation rather than in
serious code. I suspect that behavior varies from compiler (version) to
compiler (version).

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Oct 25 '05 #5
paulius-maruska wrote:
Steven T. Hatton wrote:
What should I expect the following code to print? Is it defined in the
Standard? What does it produce for you? I was kind of surprised by what
GCC 4.0.2 made of it.

As expected, it outputs:
f
a--->b


Is the first 'f' required by law? I suspect not. That is to say, I suspect
the scope of the temporary is actually the argument list. Even that may be
overstating things since I don't believe the value of the actual parameter
has a specified state vis-a-vis other actual parameters in the same
function call.

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Oct 26 '05 #6

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

Similar topics

46
by: Reinhold Birkenfeld | last post by:
Hello, another Perl/Python question: the subject says it all. Perl is going to change dramatically to become a more powerful and easier to (read|write) language. Is Python taking a similar...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
6
by: QQ | last post by:
I have a string A I'd like to print A except for for(i=offset-1;i<100;i++) printf("%c",A); or introduce a new variable like B memcpy(B,&A,100-offset) printf("%s",B); What's the easiest way...
6
by: yong321 | last post by:
With this script <script> alert(8/(3-8/3)) </script> I hope to get 24, but I get 23.99999999999999 in IE 6.0.2800 and Firefox 1.5.0.4. alert(6/(1-3/4)) returns 24 as expected. I see a...
2
by: Létező | last post by:
I use Python 2.4.4. Please read the code below: ----------------------------------------------------------- from new import classobj def mymeta(name,bases,clsdict): print 'meta: %s'%name...
5
by: consternation | last post by:
I can't find neither in tutorial nor with google It's all about isinstance, or __class__. How to test that an object is an instance of my X class?? Do I have this problems because I stre my...
16
by: Karl Kofnarson | last post by:
Hi, while writing my last program I came upon the problem of accessing a common local variable by a bunch of functions. I wanted to have a function which would, depending on some argument,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.