VC7.1 Compiler bug?

T

Tommy Andreasen

Hi,
I have the following code:

--------------------------------------
#include <iostream>

class MyObj
{
public:
MyObj()
{
std::cout << this << " MyObj constructor called\n";
}

MyObj(const MyObj&)
{
std::cout << this << " MyObj copy constructor called\n";
}

~MyObj()
{
std::cout << this << " MyObj destructor called\n";
}

operator bool()
{
return true;
}

bool isOK()
{
return true;
}
};

class Iterator
{
public:
MyObj getNext() const
{
return MyObj();
};
};

int main(int, char**)
{
Iterator iter;

while (MyObj m = iter.getNext())
{
if (m.isOK())
break;
}

return 0;
}

-------------------------------------------

When i run the program, I get the following output:

0012FF54 MyObj constructor called
0012FF54 MyObj destructor called
0012FF54 MyObj destructor called

Looks like the destructor is called twice, once after the break
statement and once after leaving the scope of the while loop.

Tommy -

tommy(dot)andreasen(at)radiometer(dot)dk
 
C

Carl Daniel [VC++ MVP]

Tommy said:
Hi,
I have the following code:

This kind of think comes up frequently, and it's usually because someone's
neglected to consider the compiler-generated copy constructor. In this
case, you've got that covered. Unless I'm missing something (entirely
possible, it's early), this is a bug in VC7.1 as well as the most current
builds of VC8.

-cd
 
?

=?ISO-8859-2?Q?Mihajlo_Cvetanovi=E6?=

Carl said:
This kind of think comes up frequently, and it's usually because someone's
neglected to consider the compiler-generated copy constructor. In this
case, you've got that covered. Unless I'm missing something (entirely
possible, it's early), this is a bug in VC7.1 as well as the most current
builds of VC8.

At the same time this won't compile:

while (MyObj m(iter.getNext()))
{
}

Neither will this:

while (bool b)
{
}

But this will compile:

while (bool b = true)
{
}

I don't know what all this means, but it doesn't look like a bug to me.
 
T

Tom Widmer [VC++ MVP]

Mihajlo said:
At the same time this won't compile:

while (MyObj m(iter.getNext()))
{
}

Nor should it according to standard C++ grammar.
Neither will this:

while (bool b)
{
}

Nor should it according to standard C++ grammar.
But this will compile:

while (bool b = true)
{
}

It should compile according to the standard. Basically, if you have a
declaration, you need an '=' for it to be legal in a for or while statement.
I don't know what all this means, but it doesn't look like a bug to me.

It does to me. From the C++ standard:

2 When the condition of a while statement is a declaration, the scope of
the variable that is declared extends from its point of declaration
(3.3.1) to the end of the while statement. A while statement of the form

while (T t = x) statement

is equivalent to

label:
{ //start of condition scope
T t = x;
if (t) {
statement
goto label;
}
} //end of condition scope

The object created in a condition is destroyed and created with each
iteration of the loop. [Example:

struct A {
int val;
A(int i) : val(i) { }
~A() { }
operator bool() { return val != 0; }
};

int i = 1;

while (A a = i) {
//...
i = 0;
}

In the while-loop, the constructor and destructor are each called twice,
once for the condition that succeeds and once for the condition that
fails. ]

Tom
 
C

Carl Daniel [VC++ MVP]

Tommy said:
Hi,
I have the following code:
[snip]

Looks like the destructor is called twice, once after the break
statement and once after leaving the scope of the while loop.

Please submit a bug report at

http://lab.msdn.microsoft.com/productfeedback/

and post a link to the report here so that others can vote on it. This bug
is still present in the very latest (two days ago) builds of VC++ 2005.

-cd
 
V

Vladimir Nesterovsky

When i run the program, I get the following output:
0012FF54 MyObj constructor called
0012FF54 MyObj destructor called
0012FF54 MyObj destructor called

Looks like the destructor is called twice, once after the break
statement and once after leaving the scope of the while loop.

I believe it's a bug in the while-break construction when while condition
contains variable declaration.

The following works well:

MyObj m = iter.getNext();

while(m)
{
if (m.isOK())
break;
}
 
C

Carl Daniel [VC++ MVP]

Carl said:
Tommy said:
Hi,
I have the following code:
[snip]

Looks like the destructor is called twice, once after the break
statement and once after leaving the scope of the while loop.

Please submit a bug report at

http://lab.msdn.microsoft.com/productfeedback/

and post a link to the report here so that others can vote on it. This bug
is still present in the very latest (two days ago) builds of
VC++ 2005.

I submitted it - here's the link:

http://lab.msdn.microsoft.com/Produ...edbackId=1d698f32-327b-4f88-acb7-5b5f9164aa24

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top