473,793 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(codo main_)
,_name(name_)
{
std::cout << _name << std::endl;
}

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

operator object () { return _codomain; }

std::ostream& print(std::ostr eam& 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 1508
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(codo main_)
,_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::ostr eam& 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********@ger mania.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(codo main_)
,_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(codo main_)
,_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::ostr eam& 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
3305
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 step (-> Python 3) some time in the near future? Reinhold
125
14858
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 software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
5
2256
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
3101
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 don't have a copy of ANSI C89 standard,therefore i had to post this question: What is the difference between "unspecified" behaviour & "undefined" behaviour of some C Code ??
6
1878
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 to do it?
6
2442
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 number of threads in this newsgroup about the floating point
2
1526
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 return classobj(name,bases,clsdict) class A(object):
5
1310
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 objects in a dict? I wrote a class X like this : class X(object): def __init__(self,name): self.name=name
16
1292
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, return other functions all having access to the same variable. An OO approach would do but why not try out closures... So here is a simplified example of the idea: def fun_basket(f):
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10161
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.