Dear All,
I'm using VC7.1.
The test program at the end of this message initializes a member
variable of reference type with a temporary. I believe the program
should output
counter = 0;
counter = 0;
indicating that the total number of objects of the temporary's type is
0 at the beginning and the end of main().
Instead, I get the output
counter = 0;
counter = 1;
indicating that the destructor of (at least one copy of) the temporary
is never called.
This program produces the expected output when compiled using Intel
8.0 for Windows, Comeau 4.3.3, GCC 3.2 (MinGW), Codewarrior 8.3 and
Borland 5.6.4.
Is this a bug, or is my program illegal for some reason? If it is a
bug, are there any known workarounds?
Best Regard,
Jonathan Turkanis
//--------Test program -------------------------//
#include <iostream>
int counter;
struct ref {
ref () { ++counter; }
ref (const ref &) { ++counter; }
~ref () { --counter; }
};
struct test {
test() : r(ref()) { }
const ref & r;
};
int main()
{
std::cout << "counter = " << counter << "\n";
{
test t;
}
std::cout << "counter = " << counter << "\n";
} 6 949
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:OK**************@TK2MSFTNGP12.phx.gbl... Dear All,
I'm using VC7.1.
The test program at the end of this message initializes a member variable of reference type with a temporary. I believe the program should output
counter = 0; counter = 0;
indicating that the total number of objects of the temporary's type is 0 at the beginning and the end of main().
Instead, I get the output
counter = 0; counter = 1;
indicating that the destructor of (at least one copy of) the temporary is never called.
This program produces the expected output when compiled using Intel 8.0 for Windows, Comeau 4.3.3, GCC 3.2 (MinGW), Codewarrior 8.3 and Borland 5.6.4.
Is this a bug, or is my program illegal for some reason? If it is a bug, are there any known workarounds?
I think it's a bug but then I'm not an expert. I thought the lifetime of the
temporary in this case was supposed to be the lifetime of the reference,
which in this case is the lifetime of t.
Workaround.... store the ref directly within test, instead of a reference.
Also why does you copy-ctor increment the count? I'm not sure that's
correct.
Stu Best Regard, Jonathan Turkanis
//--------Test program -------------------------//
#include <iostream>
int counter;
struct ref { ref () { ++counter; } ref (const ref &) { ++counter; } ~ref () { --counter; } };
struct test { test() : r(ref()) { } const ref & r; };
int main() { std::cout << "counter = " << counter << "\n"; { test t; } std::cout << "counter = " << counter << "\n"; }
>The test program at the end of this message initializes a member variable of reference type with a temporary. I believe the program should output
counter = 0; counter = 0;
You'd intuitively think that - but it'll take a language lawyer to
know for sure. It feels as though it's an ambiguous aspect to me. I
can envisage cases for both behaviours.
You could add an explicit destruction:
~test()
{
r.~ref();
}
This program produces the expected output when compiled using Intel 8.0 for Windows, Comeau 4.3.3, GCC 3.2 (MinGW), Codewarrior 8.3 and Borland 5.6.4.
I'll try to pass it on to MS since it produces the same results with
the VS2005 beta compiler.
Dave
"Stu Smith" <st*****@nospam-digita.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... "Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:OK**************@TK2MSFTNGP12.phx.gbl... Dear All,
I'm using VC7.1.
The test program at the end of this message initializes a member variable of reference type with a temporary. I believe the program should output
<snip>
Is this a bug, or is my program illegal for some reason? If it is
a bug, are there any known workarounds? I think it's a bug but then I'm not an expert. I thought the
lifetime of the temporary in this case was supposed to be the lifetime of the
reference, which in this case is the lifetime of t.
I think this falls within the first exception of 12.2/5, relating to
ctor-initlizers, so that the temporary should be destroyed when the
constructor exits. Anyway, this is what I was trying to test
Workaround.... store the ref directly within test, instead of a
reference.
This won't do what I want, unfortunately.
Also why does you copy-ctor increment the count? I'm not sure that's correct.
I want to count the total number of object creations, including
copies. Stu
Thanks,
Jonathan
"David Lowndes" <da****@example.invalid> wrote in message
news:00********************************@4ax.com... You'd intuitively think that - but it'll take a language lawyer to know for sure. It feels as though it's an ambiguous aspect to me. I can envisage cases for both behaviours.
You could add an explicit destruction:
~test() { r.~ref(); }
Unfortunately, what I was hoping I could do is get the destructor of a
derived class called without a virtual desructor, ala ScopeGuard. But
I think this case falls under the first exception in 12.2/5. This is
how the other compilers behave, anyway.
I'll try to pass it on to MS since it produces the same results with the VS2005 beta compiler.
Thanks! Dave
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl... "Stu Smith" <st*****@nospam-digita.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... "Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:OK**************@TK2MSFTNGP12.phx.gbl... Dear All,
I'm using VC7.1.
The test program at the end of this message initializes a member variable of reference type with a temporary. I believe the program should output <snip> Is this a bug, or is my program illegal for some reason? If it is a bug, are there any known workarounds? I think it's a bug but then I'm not an expert. I thought the lifetime of the temporary in this case was supposed to be the lifetime of the reference, which in this case is the lifetime of t.
I think this falls within the first exception of 12.2/5, relating to ctor-initlizers, so that the temporary should be destroyed when the constructor exits. Anyway, this is what I was trying to test
Yep I saw that too but I wasn't sure if this was the same case. Workaround.... store the ref directly within test, instead of a reference.
This won't do what I want, unfortunately.
Also why does you copy-ctor increment the count? I'm not sure that's correct.
I want to count the total number of object creations, including copies.
Do you mean the total number of creations of test? Because the ref copy-ctor
won't be called for that surely? Shouldn't the copy-ctor be on test? And
then I'd make the copy and assign methods on ref private to be sure. Stu
And if I've said something stupid, sorry, I've been using C# for the past
two years.
Thanks,
Jonathan
"Stu Smith" <st*****@nospam-digita.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... "Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:um**************@TK2MSFTNGP10.phx.gbl... "Stu Smith" <st*****@nospam-digita.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Also why does you copy-ctor increment the count? I'm not sure
that's correct. I want to count the total number of object creations, including copies.
Do you mean the total number of creations of test? Because the ref
copy-ctor won't be called for that surely? Shouldn't the copy-ctor be on test?
And then I'd make the copy and assign methods on ref private to be sure.
I wanted to count the number of refs, since I wanted to see if the
temporary created by ref() is being destroyed.
And if I've said something stupid, sorry, I've been using C# for the
past two years.
That should make you smarter. ;-)
Jonathan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sahukar praveen |
last post by:
Hello,
This is the program that I am trying. The program executes but does not give
me a desired output.
**********************************************
#include <iostream.h>
#include...
|
by: Tony Johansson |
last post by:
Hello experts!
I have this piece of code. No user defined copy constructor exist.
AccountForStudent create(long number)
{
AccountForStudent local(number, 0.0);
return local;
}
int main()
{
|
by: Michael |
last post by:
Hello,
I want to use an object (LowCut) within another object (SampleRateConverter)
like it is written as follows:
class SampleRateConverter
{
public:
SampleRateConverter( int...
|
by: AB |
last post by:
Hi All,
I've got an array of objects, during the execution of the program I'd
like to assign a particular object to a certain element in the object
array. The sample code's like this...
class...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
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...
|
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...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
| |