exception bug

exception bug

[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

2 post / 0 nuovi
Ultimo contenuto
Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione

Hello gvozdoder,

It works with latest Intel compiler (build 7.0.86). I even tried it with build 7.0.78 and it worked.

Here is the output -

$ icpc test_gvo.cpp
$ ./a.out
constructor called 61 times
destructor called 61 times
$

Please download v7.0.78 or above and let me know the results.

Regards,
CP

Lascia un commento

Eseguire l'accesso per aggiungere un commento. Non siete membri? Iscriviti oggi