473,804 Members | 3,509 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
11 1558
* Rob Williscroft:
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.


Well, first, contrary to what your quoting indicates, is was the as-if rule
I primarily referred to.

However, you're right, but possibly for the wrong reasons?, that I drew an
incorrect conclusion.

The question Old Wolf posed turns out to be answered directly by the
standard:

§3.7.2/3: "If a named automatic object has initialization or a destructor
with side effects, it shall not be destroyed before the end of its block,
nor shall it be eliminated as an optimization even if it appears to be
unused, except that a class object or its copy may be eliminated as
specified in 12.8".

So, there's the definitive formal answer, but I don't think it's wise to
rely on that behavior in an actual compiler (especially considering 12.8).

--
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 #11
Alf P. Steinbach wrote in news:42******** ********@news.i ndividual.net in
comp.lang.c++:
> 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.


Well, first, contrary to what your quoting indicates, is was the as-if
rule I primarily referred to.

However, you're right, but possibly for the wrong reasons?, that I
drew an incorrect conclusion.


I think both my highlighting of temporaries and your quote below are
needed for the picture to be complete.
The question Old Wolf posed turns out to be answered directly by the
standard:

õ3.7.2/3: "If a named automatic object has initialization or a
destructor with side effects, it shall not be destroyed before the end
of its block, nor shall it be eliminated as an optimization even if it
appears to be unused, except that a class object or its copy may be
eliminated as specified in 12.8".

So, there's the definitive formal answer, but I don't think it's wise
to rely on that behavior in an actual compiler (especially considering
12.8).


12.8/15 specifcaly mentions only 2 cases, first case creating the return
value of a function and the second case when a temporary is copied.

In the first case an rvalue (a form of temporary) is the destination
of the copy and in the second a temporary is the source of the copy.

In Old Wolf's example, both the source and destination of the copy
were named locals (in the same scope), so 12.8/15 can't apply.

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

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
4765
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
10005
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
2712
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
4005
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
4770
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
1346
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
9708
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
10589
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...
0
10340
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
10085
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...
0
9161
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
7625
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
6857
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();...
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
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.