[pre]
struct foo
{
foo();
foo(const foo &);
foo(int);
friend foo operator+(const foo &l, const foo &r);
int n;
};
int cc = 0;
int dc = 0;
main()
{
{
const foo first;
for(int i = 0; i < 20; ++i)
{
try
{
const foo second(first + i);
throw 0;
}
catch(int) {}
}
}
cout << "constructor called " << cc << " times
";
cout << "destructor called " << dc << " times
";
}
foo::foo() : n(0) { ++cc; }
foo::foo(const foo &r) : n(r.n) { ++cc; }
foo::foo(int m) : n(m) { ++cc; }
foo::~foo() { ++dc; }
foo operator+(const foo &l, const foo &r) { return foo(l.n + r.n); }
[/pre]
I got a cc==61 and dc==81 with Intel C++ 7.0 (build 073). I think that object (first+i) was destructed twice during each for-loop iteration.
Is this a problem in my code or in a compiler? Are there any workarounds?
PS. Sorry for my bad english
exception bug
For more complete information about compiler optimizations, see our Optimization Notice.


