473,795 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

which on is correct?~about temp object~

hi, all,
i'm reading ch.20 -smart pointers- of [C++ Template].
and i'm tring the trule.hpp test. but there's something different
than i expect, and i found that's about temp object. so i simplified
the question into the following code:

===code starts===
#include <iostream>
using namespace std;

class H
{
public:
H(int i){ cout << "h" << endl; }
};

class T
{
public:
T(int i){ cout << "t" << endl; }
T(H& h){ cout << "ht" << endl; }
T(T const & rhs){ cout << "tt" << endl; }
};

T foo()
{
int i = 10;
return i;
}

T bar()
{
H h(11);
return h;
}

int main()
{
T t1(foo());
T t2(bar());
return 0;
}
===code ends===

on my win2k box, i both use bcb and devc++ to test it:
===bcb output===
t
tt
h
ht
tt
===devc++ output===
t
h
ht
=============

well, i though bcb was right, cause both foo() and bar() generate
temp obj, and which should be use to copy contruct the return T.

anyway, i want to know which is correct, i mean, std compliant.
if it's not bcb, why?

thanks.

Jul 23 '05 #1
11 1556
cf****@gmail.co m wrote:
i'm reading ch.20 -smart pointers- of [C++ Template].
and i'm tring the trule.hpp test. but there's something different
than i expect, and i found that's about temp object. so i simplified
the question into the following code:

===code starts===
#include <iostream>
using namespace std;

class H
{
public:
H(int i){ cout << "h" << endl; }
};

class T
{
public:
T(int i){ cout << "t" << endl; }
T(H& h){ cout << "ht" << endl; }
T(T const & rhs){ cout << "tt" << endl; }
};

T foo()
{
int i = 10;
return i;
}

T bar()
{
H h(11);
return h;
}

int main()
{
T t1(foo());
T t2(bar());
return 0;
}
===code ends===

on my win2k box, i both use bcb and devc++ to test it:
===bcb output===
t
tt
h
ht
tt
===devc++ output===
t
h
ht
=============

well, i though bcb was right, cause both foo() and bar() generate
temp obj, and which should be use to copy contruct the return T.

anyway, i want to know which is correct, i mean, std compliant.
if it's not bcb, why?


I think both are OK. During initialisation, the compiler is allowed
to forgo creation of a temporary like that and initialise directly into
the object. How they do it? I don't know. Examine the assembly code
and you will probably find out that most of the member functions have
been inlined and the initialisation goes directly into the object
without copying.

May be worth to mention that while compiler is allowed to skip the
creation of a temporary in certain contexts, the possibility should
still exist. For example, if you declare the copy constructor for
your class T private, the code shouldn't compile even with devc++
since the copy cannot be made (even though the compiler can create
code that doesn't require creation of a copy).

V
Jul 23 '05 #2
* cf****@gmail.co m:
[snip example]
anyway, i want to know which is correct, i mean, std compliant.
if it's not bcb, why?


As an alternative explanation to Victor's, you might choose to think about
this as primarily being about copy constructors, not temporaries.

A copy constructor is the only member function that is required to do one
thing and one thing only: to copy the argument into the current
to-be-constructed object.

The compiler is allowed to assume that a copy constructor does what it's
supposed to, not more, and not less. And on that basis -- without
considering the actual code you've placed in in a copy constructor -- the
compiler can replace or remove calls if that leaves the final effect
unchanged. So if a copy constructor does something more than it's supposed
to, then those actions are not guaranteed to be executed.

Thus, any C++ questionnare that asks you to count the number of copy
constructor calls is questionable... ;-)

And so are attempts at optimizing away copy constructor calls. Typically
the simplest code will be optimized very well by the compiler, those darned
copy operations removed. But "clever" code can end up with no optimization.

--
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?
Jul 23 '05 #3
Alf P. Steinbach wrote:

The compiler is allowed to assume that a copy constructor does what
it's supposed to, not more, and not less. And on that basis --
without considering the actual code you've placed in in a copy
constructor -- the compiler can replace or remove calls if that
leaves the final effect unchanged. So if a copy constructor does
something more than it's supposed to, then those actions are not
guaranteed to be executed.


I think that only applies to RVO and NRVO. In other cases,
the compiler must execute all of the copy constructor's
side effects etc. If it calls the copy constructor multiple times,
then it must do the side effects multiple times.

Jul 23 '05 #4
* Old Wolf:
Alf P. Steinbach wrote:

The compiler is allowed to assume that a copy constructor does what
it's supposed to, not more, and not less. And on that basis --
without considering the actual code you've placed in in a copy
constructor -- the compiler can replace or remove calls if that
leaves the final effect unchanged. So if a copy constructor does
something more than it's supposed to, then those actions are not
guaranteed to be executed.


I think that only applies to RVO and NRVO. In other cases,
the compiler must execute all of the copy constructor's
side effects etc. If it calls the copy constructor multiple times,
then it must do the side effects multiple times.


As a simple non-RVO/NRVO example,

SomeClass x = SomeClass( 123 );

Here the temporary and copy constructor can be elided, according to our holy
standard.

And so on.
Cheers,

- Alf

--
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?
Jul 23 '05 #5
Alf P. Steinbach schreef:
any C++ questionnare that asks you to count the number of copy
constructor calls is questionable... ;-)


Well, there are two situations that are definitely distinct:
==0 and >=0. The first doesn't require an accessible copy ctor.
The second one does.

I'm afraid the number of quizzes that have these options
is a bit low, though.

Regards,
Michiel Salters

Jul 23 '05 #6
sorry, i don't really get it. may you explain the two situations,
thanks a lot.
i reviewed the "inside the c++ object model", which deeply explains
copy constructor and NRV.

And I hope the following conclusion is right:
the NRV is used to improve efficiency without actually invoking the
copy constructor.
and for Als' example:
SomeClass x = SomeClass( 123 );
if NRV is applied, it won't be:
SomeClass temp.SomeClass: :SomeClass(123) ;
SomeClass x.SomeClass::So meClass(temp); //copy constructor
instead, it'll be:
SomeClass x.SomeClass::So meClass(123); //x substitutes temp, and
construct directly in the x's space

thank you.

Jul 23 '05 #7
Alf P. Steinbach wrote:
* Old Wolf:
Alf P. Steinbach wrote:

The compiler is allowed to assume that a copy constructor does what
it's supposed to, not more, and not less. And on that basis --
without considering the actual code you've placed in in a copy
constructor -- the compiler can replace or remove calls if that
leaves the final effect unchanged. So if a copy constructor does
something more than it's supposed to, then those actions are not
guaranteed to be executed.


I think that only applies to RVO and NRVO. In other cases,
the compiler must execute all of the copy constructor's
side effects etc. If it calls the copy constructor multiple times,
then it must do the side effects multiple times.


As a simple non-RVO/NRVO example,

SomeClass x = SomeClass( 123 );


How about:

void foo(SomeClass const &);

int main()
{
SomeClass a;
SomeClass b(a);
foo(b);
}

Can that copy-constructor and side-effect be elided because
the compiler determines that 'a' is never used again, and
it decides to simply use 'b' and 'a' to both refer to the same
bit of memory? If not, why not?

Jul 27 '05 #8
* Old Wolf:

How about:

void foo(SomeClass const &);

int main()
{
SomeClass a;
SomeClass b(a);
foo(b);
}

Can that copy-constructor and side-effect be elided because
the compiler determines that 'a' is never used again, and
it decides to simply use 'b' and 'a' to both refer to the same
bit of memory?


I could just give an answer, pretending to absolutely know such details, but
I think it's more honest to explain the process towards that answer.

1. I read your question and I'm _fairly sure_ the answer is yes, because of
what's commonly called the "as if" rule, and I'm planning to mention the
rather non-obvious requirement for a copy constructor, generated or
user-defined, when passing by ref to const (discovered during AA's Mojo
adventure), and also the difference when a and b's adresses might be used.

2. I plan to check the 1998 standard. But first, I make some coffee.

3. I discover that I'm almost out of coffee, and I've just been down to 7-11
and not very keen to make another trip, it takes twelve minutes each way.
What's needed to cancel the spiritual effects of this severe psychological
setback is evidently a bite of "Gullbrød" chocolate, favoured by our Prime
Minister. "Gullbrød", wolfed down (hah), one more cup of coffee, down.

4. Acrobat Reader informs me there's an update available. And not just one!
However, no matter what is selected it insists on also installing Yahoo
Toolbar. F**k off, Adobe. I don't want no sneaking malware Yahoo Toolbar,
_especially not_ one that cannot be deselected in the installation dialog.

5. OK, the "as if" rule, that's §1.9/1, "confirming implementations are
required to emulate (only) the observable behavior of the abstract machine
as explained below", where the key word is _observable_. §1.9/6 then
defines observable behavior as the "sequence of reads and writes to volatile
data and calls to library I/O functions", with a note explaining that
library I/O functions include such functions added by the implementation.
So already, given that copy constructors can be elided without regard to
their side-effects, this provides the answer "yes" to your question. But I
gather you're interested also in the copy constructor side-effect issue.

6. The possibility of ignoring side-effects in copy constructors is
mentioned twice in e.g. §12.8/15, about eliding temporaries for
initialization and function results, "even if the copy constructor or
destructor have side effects". So there I learned something new. Even
though it was implicit in what I already knew I hadn't really thought about
a destructor with side-effects, but there you are: neither copy constructor
nor destructor side-effects can be relied on in C++. It's all Andrei
Alexandrescu's fault, because as I recall he wrote in MC++D that the copy
constructor was special in this regard. But it's also destructors.

7. Regarding the requirement for a copy constructor to exist for passing by
reference to const, well, I don't find it. But I'm fairly sure it's there,
somewhere, even for C++2003. And also that it will be fixed in C++0x.

--
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?
Jul 27 '05 #9
Alf P. Steinbach wrote in news:42******** ********@news.i ndividual.net in
comp.lang.c++:
* Old Wolf:

How about:

void foo(SomeClass const &);

int main()
{
SomeClass a;
SomeClass b(a);
foo(b);
}

Can that copy-constructor and side-effect be elided because
the compiler determines that 'a' is never used again, and
it decides to simply use 'b' and 'a' to both refer to the same
bit of memory?


6. The possibility of ignoring side-effects in copy constructors is
mentioned twice in e.g. §12.8/15, about eliding temporaries for
initialization and function results,


The keyword there is "temporarie s", in the above neither "a" nor "b"
is a temporary so the copy-construction of "b" can't be elided, except
under the as-if rule, which can't happen if the copy-constructor (or
destructor, as you point out in the bit I snipped) has side effects.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 27 '05 #10

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

Similar topics

3
4159
by: Victor | last post by:
I'm trying to run this java program, but somehow the program always quit w/o giving any error msg at all. it happenned inside the first case statements. Strangely, after printing happen2, it just stopped, and I had no idea what happens. another error is on the last function. I have already declared plane as M x N array, but it keeps giving error like hwone.cpp(50) : warning C4101: 'plane' : unreferenced local variable hwone.cpp(212) :...
3
4764
by: Duncan | last post by:
I need to populate a vector with the following struct details:\ struct group { string groupname; int gid; list<string> members; }; the text file which this is to read from is in the following form:
6
10004
by: anirban.anirbanju | last post by:
hi there, i've some serious problem to add rows dynamically in a table. my table contains 5 cell. | check | from_value | to_value | color_text | color_value | --------------------------------------------------------------------------------- | | | | | | | | | | |
11
2709
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h> #include <string.h> #define LENGTH 20 int temp, m, n, i, r, base10, true;
6
1390
by: polas | last post by:
Hello all - I have a question which is perhaps a little unusual. I am creating a language translator which uses C as the target language. In C, when calling a function and passing variables as arguments in, I wish to pass a pointer of the variable to the function. However, due to internals of the language translator it will be difficult to mark when variables are in fact pointers and need a * prefix. I was therefore wondering if there is a...
9
4003
by: seep | last post by:
hi i m finding following error on the code that i wants to use to get all record from table via store procedure with paging. the error is : Input string was not in a correct format. after a hectic struggle still i dont know 1--who can i solve it and 2--where should i have to place the function GETDATA . who is it possible to keep all the functions in a separate file and to call that file in required page. here is the...
2
1574
by: king imran | last post by:
////////////////////////////// LinkList.cpp #include "LinkList.h" /* The LinkList class implementation*/ /* Constructor */ LinkList::LinkList()
1
4769
by: differentsri | last post by:
THIS IS AN ASP.NET 1.1 APPLICATION IAM TRYING TO UPDATE THE FIELD BUT I AM NOT ABLE TO UPDATE IT? CAN U TELL THE REASON ? IT IS GIVING THE FOLLOWING ERROR BELOW I HAVE ALSO GIVEN THE CODE OF THE PROGRAM PLEASE HELP ME Server Error in '/WebApplication1' Application. -------------------------------------------------------------------------------- Input string was not in a correct format. Description: An unhandled exception occurred...
3
1345
by: raghulvarma | last post by:
I did a small sample with three tier architecture and i need to know whether the flow of the program is correct or not.The pgm works fine. Inside web.config <configuration> <appSettings> <add key="Connection" value="server=.;trusted_connection=true;initial catalog=sanjay"/> </appSettings></configuration> Inside Data Access Layer private string username; private string password; public string UserName
0
9673
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
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10002
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
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
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
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...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.