Yes, I was indeed in a bad mood yesterday. I appologize for anything bad I have said.
Calling references to Microsoft's docs, Open Group's IEEE standard docs, and this site's docs "crap" does little to advance ISO references --as they strictly conform to the ISO standards. |
Your first reference is to the *C* langauage specs. There are differences. C++ was never fully compatible, nor was it meant to be. This is such a case (from the C++ standard, before the description of exit(): "The contents are the same as the Standard C library header <stdlib.h>, with the following changes:")
Calling MS-docs 'crap' is something I've learned from the US courts. And all kidding aside, after what they have done to C++ with their 6.x VC++ versions (the only one I worked with), I don't give a damn what they consider right. (Don't get me wrong, I don't want to make this into a pro/con Microsoft war. But I think we can agree on the fact that they have their problems with standards. Just look at OOXML.)
As for your reference to this site, well, it doesn't conform with ISO 14882. I would certainly like to know where the author has taken his knowledge from. This is the very least thing one could expect from any reference for a standardized language/whatever.
All in all, having looked at the ISO C++ standard again, I now am pretty sure that most people have confused "static" with "automatic". *static* variables are indeed cleaned up. But not automatic ones.
I didn't make that up, regardless of whether you want to believe it. |
Despite my mood, I didn't say (or mean) that. Obviously, on a board, it serves little purpose to say "Godd answer" when someone is right. On the other hand, if something suboptimal, non-portable, or even wrong is posted, a correction is a Good Thing. So please, don't interprete my posts as insults or the attitude "I don't like you and am better than you, so shut up" or something similar. I merely wanted to correct what was said about a language detail.
When would be a good time to use the exit()? |
As of ISO 14882: when you don't have any objects with automatic storage duration and all objects with dynamic storage duration have been destroyed (or will be destroyed by a function registred with atexit()).
Oh. And one last thing. You are of course right that the standard is not as important as the implementations. Just try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <cstdlib>
#include <iostream>
struct C
{
~C()
{
std::cout<<"X"<<std::endl;
}
};
void f()
{
C c;
exit(1);
}
int main()
{
f();
}
| |
Note, that while the C-header "stdlib.h" instead of <cstdlib> would be required to clean variables with automatic storage duration, however, in my implementation, it doesn't call destructors (I'm pretty sure, though cannot prove, that this is intended, because C had no destructors and cleaning up auto variables was something completely different). On my platform, C's destructor isn't called in both cases.