Click to See Complete Forum and Search --> : std namespace


asxetos
December 19th, 2005, 04:12 AM
I have bought a book for C++ and in some examples has the following code:

#include <cstdlib>
using std::exit;

int main()
{
.....exit(1);
}

I use Visual C++ 6 and when I compile this code it produces this error:
"error C2039: 'exit' : is not a member of 'std' "

When I delete " using std::exit",the source code is compiled without any errors.

Why is that?Microsoft Visual C++ 6 defines in a different way the std namespace so as not to include exit?

Siddhartha
December 19th, 2005, 04:18 AM
You don't need to use exit here. In fact, one really doesn't need exit.

Simply return.

int main ()
{
return 0;
}

asxetos
December 19th, 2005, 04:22 AM
No,I need to use exit(1).It is used for program termination when a specific error occurs.

What about the problem I initially stated?

Siddhartha
December 19th, 2005, 04:33 AM
No,I need to use exit(1). When an error occurs, return error, or throw an exception (caught by the caller who optionally returns). There is literally no case I can think of where one might need to rudely terminate an application using exit.

Calling exit (http://msdn2.microsoft.com/en-us/library/6wdz5232.aspx) is a poor programming practise -
When you call the exit or _exit functions, the destructors for any temporary or automatic objects that exist at the time of the call are not called. And...
What about the problem I initially stated?What about it?

EDIT: exit is a function inherited from C, and is a member of the standard namespace on my version of VC++ 8.0 -
#include <cstdlib>

int main ()
{
std::exit (0);
}