By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,512 Members | 1,814 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,512 IT Pros & Developers. It's quick & easy.

What do you expect this to print?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
* 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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.