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 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

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

Laisser un commentaire

Veuillez ouvrir une session pour ajouter un commentaire. Pas encore membre ? Rejoignez-nous dès aujourd’hui