 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ioannis Vranos Guest
|
Posted: Thu Apr 15, 2004 5:10 pm Post subject: return 1; suggestion |
|
|
Now it may sound a bit stuoid in the beginning. I was talking with someone
who was using return 1; in main() to signal errors because
#include <cstdlib>
return EXIT_FAILURE;
is tiresome to write and also difficult for newbies to understand it.
Wouldn't be better this to be added in the upcoming standard as a portable
way ti signal error? Essentially return 1; to be equivalent to return
EXIT_FAILURE; as return 0; is equivalent to return EXIT_SUCCESS;
Ioannis Vranos
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Thu Apr 15, 2004 7:48 pm Post subject: Re: return 1; suggestion |
|
|
"Ioannis Vranos" wrote:
| Quote: | Now it may sound a bit stuoid in the beginning. I was talking with someone
who was using return 1; in main() to signal errors because
#include
return EXIT_FAILURE;
is tiresome to write
|
Oh, the agony. You only have to write it once per program.
| Quote: | and also difficult for newbies to understand it.
|
The #include may be obscure, but that's something that has to be
learnt anyway. The named constant is surely helpful to "newbies" and
others. It's not exactly obvious that 0 means success and 1 means
failure; in fact it's the inverse of what one might expect.
| Quote: | Wouldn't be better this to be added in the upcoming standard as a portable
way ti signal error? Essentially return 1; to be equivalent to return
EXIT_FAILURE; as return 0; is equivalent to return EXIT_SUCCESS;
|
That might break a platform-specific C++ program that currently
returns 1 on a platform where this doesn't indicate failure. For
example, on VMS a return code of 1 indicates success. (I suspect that
C++ implementations on VMS change a return code of 0 to 1 and vice
versa, but this certainly isn't required.)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
James Kuyper Guest
|
Posted: Thu Apr 15, 2004 11:09 pm Post subject: Re: return 1; suggestion |
|
|
[email]ivr (AT) guesswh (DOT) at.emails.ru[/email] ("Ioannis Vranos") wrote in message news:<c5m7ap$1f25$1 (AT) ulysses (DOT) noc.ntua.gr>...
| Quote: | Now it may sound a bit stuoid in the beginning. I was talking with someone
who was using return 1; in main() to signal errors because
#include
return EXIT_FAILURE;
is tiresome to write and also difficult for newbies to understand it.
|
He finds "return 1;" to be easier to understand than "return
EXIT_FAILURE;"? The second clearly implies that you're exiting the
program, and that something failed. I have no idea what a newbie would
conclude from "return 1", but it's rather unlikely to match what I
just wrote.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Loïc Joly Guest
|
Posted: Thu Apr 15, 2004 11:25 pm Post subject: Re: return 1; suggestion |
|
|
Ioannis Vranos wrote:
| Quote: | Now it may sound a bit stuoid in the beginning. I was talking with someone
who was using return 1; in main() to signal errors because
#include
return EXIT_FAILURE;
is tiresome to write and also difficult for newbies to understand it.
Wouldn't be better this to be added in the upcoming standard as a portable
way ti signal error? Essentially return 1; to be equivalent to return
EXIT_FAILURE; as return 0; is equivalent to return EXIT_SUCCESS;
|
Well, I do agree the there is an inconsistency between the success and
the failure case, and that this inconsistency can make learning C++ a
(very) little bit more complex.
My proposal would be just the opposite of yours : Just don't mandate any
value for EXIT_SUCCESS.
--
Loïc
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ioannis Vranos Guest
|
Posted: Fri Apr 16, 2004 12:59 am Post subject: Re: return 1; suggestion |
|
|
"James Kuyper" <kuyper (AT) wizard (DOT) net> wrote
| Quote: | ivr (AT) guesswh (DOT) at.emails.ru ("Ioannis Vranos") wrote in message
news:<c5m7ap$1f25$1 (AT) ulysses (DOT) noc.ntua.gr>...
Now it may sound a bit stuoid in the beginning. I was talking with
someone
who was using return 1; in main() to signal errors because
#include <cstdlib
return EXIT_FAILURE;
is tiresome to write and also difficult for newbies to understand it.
He finds "return 1;" to be easier to understand than "return
EXIT_FAILURE;"? The second clearly implies that you're exiting the
program, and that something failed. I have no idea what a newbie would
conclude from "return 1", but it's rather unlikely to match what I
just wrote.
|
It is easier for a newbie to write return 1; rather than #include
... return EXIT_FAILURE; When you were newbie you were returning
EXIT_FAILURE; in main() if you wanted to signal failure?
Ioannis Vranos
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Niklas Matthies Guest
|
Posted: Fri Apr 16, 2004 12:59 am Post subject: Re: return 1; suggestion |
|
|
On 2004-04-15 23:09, James Kuyper wrote:
:
| Quote: | He finds "return 1;" to be easier to understand than "return
EXIT_FAILURE;"? The second clearly implies that you're exiting the
program, and that something failed.
|
Actually, I tend to think that EXIT_FAILURE clearly implies a failure
to exit (whatever is or was being attempted to be exited). ;)
-- Niklas Matthies
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
James Kuyper Guest
|
Posted: Fri Apr 16, 2004 3:28 pm Post subject: Re: return 1; suggestion |
|
|
[email]ivr (AT) otenet (DOT) gr[/email] ("Ioannis Vranos") wrote in message news:<c5n7mn$qq9$1 (AT) ulysses (DOT) noc.ntua.gr>...
| Quote: | "James Kuyper" <kuyper (AT) wizard (DOT) net> wrote in message
news:8b42afac.0404151417.3383656a (AT) posting (DOT) google.com...
[email]ivr (AT) guesswh (DOT) at.emails.ru[/email] ("Ioannis Vranos") wrote in message
news:<c5m7ap$1f25$1 (AT) ulysses (DOT) noc.ntua.gr>...
Now it may sound a bit stuoid in the beginning. I was talking with
someone
who was using return 1; in main() to signal errors because
#include
return EXIT_FAILURE;
is tiresome to write and also difficult for newbies to understand it.
He finds "return 1;" to be easier to understand than "return
EXIT_FAILURE;"? The second clearly implies that you're exiting the
program, and that something failed. I have no idea what a newbie would
conclude from "return 1", but it's rather unlikely to match what I
just wrote.
It is easier for a newbie to write return 1; rather than #include
.. return EXIT_FAILURE; When you were newbie you were returning
EXIT_FAILURE; in main() if you wanted to signal failure?
|
I started using EXIT_FAILURE as soon as I heard of it. That was so
long ago that I can't remember whether it was part of the K&R C that I
first learned, or was an ANSI innovation. If the latter, then it
didn't come into existence until long after I was no longer a newbie.
On the other hand, it was a long time before I realized that "return
EXIT_FAILURE;" was somewhat better than "exit(EXIT_FAILURE);". I don't
remember when I made that transition.
You said it was "difficult for newbies to understand"; now you're
arguing that it's difficult for them to write. Those two different
things. I agree that a newbie might be unfamiliar with the
EXIT_FAILURE macro; a newbie is equally likely to be unfamiliar with
the (implementation-specific) fact that "return 1;" can be used for
that same purpose. Howver, as far as understanding is concerned,
rather than writing, EXIT_FAILURE is wonderfully self-expressive, and
correspondingly easy to understand, making it far superior to '1'.
It's also guaranteed to return an error status, which isn't true of
'1'.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Dave Moore Guest
|
Posted: Fri Apr 16, 2004 5:40 pm Post subject: Re: return 1; suggestion |
|
|
snip
| Quote: |
It is easier for a newbie to write return 1; rather than #include <cstdlib
.. return EXIT_FAILURE; When you were newbie you were returning
EXIT_FAILURE; in main() if you wanted to signal failure?
|
Hmmm ... I think the logic is backward here ... "easier" doesn't
necessarily mean "better", *especially* for newbies. I agree with
other posters that the named variable is far clearer, even if it
involves a few more keystrokes. When I was a newbie, I understood the
value of name-substitution (e.g. via typedef) for literals, long
before I understood the (arbitrary) convention that <0> means
"success" and <any other int> means failure when returned by a
process. Furthermore, I continue to find it confusing that this is
the opposite of the "natural" int equivalents for booleans, where
false=0 and true=1 (or any other non-zero value). Perhaps this is not
universal, but I find it logical to assume that the pairs (false,
failure), and (true, success) should be correlated, rather than the
reverse.
Finally, why convolute the standard for the sake of newbies? By
definition, they are people who need to learn more about the language
.. and making mistakes (like returning 1) is the best way to do this.
In fact, if there were going to be a change made, I would rather see
them get rid of the fixed meaning of {return 0;} for success, and
mandate {return EXIT_SUCCESS;} in its place.
Just my 2 cent ...
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Falk Tannhäuser Guest
|
Posted: Fri Apr 16, 2004 5:40 pm Post subject: Re: return 1; suggestion |
|
|
Ioannis Vranos wrote:
| Quote: |
"James Kuyper" <kuyper (AT) wizard (DOT) net> wrote in message
He finds "return 1;" to be easier to understand than "return
EXIT_FAILURE;"? The second clearly implies that you're exiting the
program, and that something failed. I have no idea what a newbie would
conclude from "return 1", but it's rather unlikely to match what I
just wrote.
It is easier for a newbie to write return 1; rather than #include
.. return EXIT_FAILURE; When you were newbie you were returning
EXIT_FAILURE; in main() if you wanted to signal failure?
I believe that one thing a newbie should learn is that, in many cases, |
it is preferable to have a program that is easy to /read/ than one
that it is easy to /write/.
'EXIT_FAILURE' is clearly more descriptive about the programmer's intention
than '1', even if it requires more keystrokes.
Falk
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Sat Apr 17, 2004 5:36 pm Post subject: Re: return 1; suggestion |
|
|
[email]ivr (AT) guesswh (DOT) at.emails.ru[/email] ("Ioannis Vranos") writes:
| Quote: | Now it may sound a bit stuoid in the beginning. I was talking with someone
who was using return 1; in main() to signal errors because
#include
return EXIT_FAILURE;
is tiresome to write and also difficult for newbies to understand it.
Wouldn't be better this to be added in the upcoming standard as a portable
way ti signal error? Essentially return 1; to be equivalent to return
EXIT_FAILURE; as return 0; is equivalent to return EXIT_SUCCESS;
|
Do you realize that making 'return 1;' equivalent to
'return EXIT_FAILURE;' would make 'return true;' equivalent to
'return EXIT_FAILURE;' as well?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ioannis Vranos Guest
|
Posted: Sat Apr 17, 2004 8:59 pm Post subject: Re: return 1; suggestion |
|
|
"llewelly" <llewelly.at (AT) xmission (DOT) dot.com> wrote
| Quote: | ivr (AT) guesswh (DOT) at.emails.ru ("Ioannis Vranos") writes:
Do you realize that making 'return 1;' equivalent to
'return EXIT_FAILURE;' would make 'return true;' equivalent to
'return EXIT_FAILURE;' as well?
|
While now returning true in main() is undefined behaviour.
Ioannis Vranos
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Sun Apr 18, 2004 9:09 pm Post subject: Re: return 1; suggestion |
|
|
[email]ivr (AT) guesswh (DOT) at.emails.ru[/email] ("Ioannis Vranos") writes:
| Quote: | "llewelly" <llewelly.at (AT) xmission (DOT) dot.com> wrote in message
news:86n05bcmbb.fsf (AT) Zorthluthik (DOT) local.bar...
[email]ivr (AT) guesswh (DOT) at.emails.ru[/email] ("Ioannis Vranos") writes:
Do you realize that making 'return 1;' equivalent to
'return EXIT_FAILURE;' would make 'return true;' equivalent to
'return EXIT_FAILURE;' as well?
While now returning true in main() is undefined behaviour.
[snip] |
I would like to see it become a required diagnostic. I'm chary of how
much code that would break, however. And it doesn't seem terribly
important.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Mon Apr 19, 2004 4:08 pm Post subject: Re: return 1; suggestion |
|
|
""Ioannis Vranos"" <ivr (AT) guesswh (DOT) at.emails.ru> wrote
| Quote: | Do you realize that making 'return 1;' equivalent to
'return EXIT_FAILURE;' would make 'return true;' equivalent to
'return EXIT_FAILURE;' as well?
While now returning true in main() is undefined behaviour.
|
Really? Why do you think so?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Michiel Salters Guest
|
Posted: Mon Apr 19, 2004 5:35 pm Post subject: Re: return 1; suggestion |
|
|
[email]ivr (AT) guesswh (DOT) at.emails.ru[/email] ("Ioannis Vranos") wrote in message news:<c5s2i1$7ub$1 (AT) ulysses (DOT) noc.ntua.gr>...
| Quote: | "llewelly" <llewelly.at (AT) xmission (DOT) dot.com> wrote in message
news:86n05bcmbb.fsf (AT) Zorthluthik (DOT) local.bar...
[email]ivr (AT) guesswh (DOT) at.emails.ru[/email] ("Ioannis Vranos") writes:
Do you realize that making 'return 1;' equivalent to
'return EXIT_FAILURE;' would make 'return true;' equivalent to
'return EXIT_FAILURE;' as well?
While now returning true in main() is undefined behaviour.
No. true is converted to 1, which means main() returns 1. This is |
equivalent to exit(1). The meaning of exit(1) is implementation-
defined per 18.3/8, and not undefined.
Regards,
Michiel Salters
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Mon Apr 19, 2004 5:37 pm Post subject: Re: return 1; suggestion |
|
|
[email]ark (AT) acm (DOT) org[/email] ("Andrew Koenig") writes:
| Quote: | ""Ioannis Vranos"" <ivr (AT) guesswh (DOT) at.emails.ru> wrote in message
news:c5s2i1$7ub$1 (AT) ulysses (DOT) noc.ntua.gr...
Do you realize that making 'return 1;' equivalent to
'return EXIT_FAILURE;' would make 'return true;' equivalent to
'return EXIT_FAILURE;' as well?
While now returning true in main() is undefined behaviour.
Really? Why do you think so?
|
hm, the 3rd bullet of 18.3/8 says it is implementation-defined
behaviour. I'd forgotten that.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|