Definition of logical true in C++?

Discussion in 'C++' started by Lars Uffmann, Jan 21, 2008.

  1. Lars Uffmann

    Lars Uffmann Guest

    If I define a function, that shall return -1 in case of error, and a
    positive value otherwise:

    int foo (int param) {
    if (outOfRange (param))
    return -1;

    return (param != 0);
    }

    can I rely on this code always working, or is it possible that the
    return statement produces a -1 also, on a specific compiler / OS?
    Is this defined somewhere? And if so, where in the standard?

    Best Regards,

    Lars
     
    Lars Uffmann, Jan 21, 2008
    #1
    1. Advertising

  2. Lars Uffmann wrote:


    > can I rely on this code always working, or is it possible that the
    > return statement produces a -1 also, on a specific compiler / OS?
    > Is this defined somewhere? And if so, where in the standard?


    As defined by 4.5/4, boolean true is promoted to 1, boolean false to 0.


    --
    Sebastian Redl
     
    Sebastian Redl, Jan 21, 2008
    #2
    1. Advertising

  3. Lars Uffmann

    Lars Uffmann Guest

    Sebastian Redl wrote:
    > As defined by 4.5/4, boolean true is promoted to 1, boolean false to 0.


    Thanks, that's all I needed to know - though I just noticed that there's
    no easily found online version of the C++ standards - are you able to
    point me to one?

    Best Regards,

    Lars
     
    Lars Uffmann, Jan 21, 2008
    #3
  4. Lars Uffmann

    Ian Collins Guest

    Lars Uffmann wrote:
    > Sebastian Redl wrote:
    >> As defined by 4.5/4, boolean true is promoted to 1, boolean false to 0.

    >
    > Thanks, that's all I needed to know - though I just noticed that there's
    > no easily found online version of the C++ standards - are you able to
    > point me to one?
    >

    http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS/ISO/IEC 14882-2003

    --
    Ian Collins.
     
    Ian Collins, Jan 21, 2008
    #4
  5. Lars Uffmann

    Lars Uffmann Guest

    Lars Uffmann, Jan 21, 2008
    #5
  6. Lars Uffmann wrote:

    > Ian Collins wrote:
    >>

    http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS/ISO/IEC 14882-2003
    >
    > Hmm - so the standard for THE programming language (imho) is not freely
    > available?


    That's correct. ISO makes some money by selling the standard documents.

    However, the drafts of the standard are freely available (search for C++
    last public draft) and differ from the final version only in some rather
    small issues. (The biggest difference I've found so far is in the rules
    concerning reinterpret_cast.)

    --
    Sebastian Redl
     
    Sebastian Redl, Jan 21, 2008
    #6
  7. Lars Uffmann

    Lars Uffmann Guest

    Lars Uffmann, Jan 21, 2008
    #7
  8. Lars Uffmann

    Lars Uffmann Guest

    C++ Standard hardcopy (was: Re: Definition of logical true in C++?)

    Sebastian Redl wrote:
    > Lars Uffmann wrote:
    >
    >> Ian Collins wrote:

    > http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS/ISO/IEC 14882-2003
    >> Hmm - so the standard for THE programming language (imho) is not freely
    >> available?

    >
    > That's correct. ISO makes some money by selling the standard documents.


    Okay, I guess I don't mind... Thanks for the hint with the drafts.
    As for a hardcopy of the standard - all I seem to be able to find is
    http://www.amazon.com/C++-Standard-...bs_sr_1?ie=UTF8&s=books&qid=1200903548&sr=1-1
    - is there any other book that I've been overlooking?

    Thanks!

    Lars
     
    Lars Uffmann, Jan 21, 2008
    #8
  9. * Lars Uffmann:
    > Lars Uffmann wrote:
    >> Ian Collins wrote:
    >>> http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS/ISO/IEC 14882-2003

    >>
    >> Not that I mind ordering a copy... just wondrin'... *headscratch*

    >
    > Argl... That license agreement says "single computer only"... I guess
    > I'll go for a printed book version :) Amazon, here I come...


    In order to use the standard, you really need a searchable document.

    Without searchability, all you have is a pile of expensive paper,
    because relevant paragraphs for some issue are generally scattered
    throughout the standard -- because the standard has evolved from a
    relatively small document, keeping the document structure while adding
    in new stuff and amendning existing text here and there (it really needs
    quite a bit of refactoring! :) ).

    I recommend downloading the latest draft from the committee pages (it's
    in PDF format), and familiarizing yourself with use of that, before
    paying hard cash for flaked dead trees.


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 21, 2008
    #9
  10. Lars Uffmann

    Lars Uffmann Guest

    Alf P. Steinbach wrote:
    > In order to use the standard, you really need a searchable document.

    Valid point, the thing is, I need the standard as a reference for my
    department within the german space agency. A 1-workstation-license isn't
    really doing us much good.

    > I recommend downloading the latest draft from the committee pages (it's
    > in PDF format), and familiarizing yourself with use of that, before
    > paying hard cash for flaked dead trees.


    I am still having a hard time finding a pdf of the latest draft - C++98
    is what I need I guess - and the committee seems to be going out of its
    way to hide any download links very well deep down in the site... I'll
    keep searching..

    Best Regards,

    Lars
     
    Lars Uffmann, Jan 21, 2008
    #10
  11. * Lars Uffmann:
    > Alf P. Steinbach wrote:
    >> In order to use the standard, you really need a searchable document.

    > Valid point, the thing is, I need the standard as a reference for my
    > department within the german space agency. A 1-workstation-license isn't
    > really doing us much good.
    >
    >> I recommend downloading the latest draft from the committee pages
    >> (it's in PDF format), and familiarizing yourself with use of that,
    >> before paying hard cash for flaked dead trees.

    >
    > I am still having a hard time finding a pdf of the latest draft - C++98
    > is what I need I guess - and the committee seems to be going out of its
    > way to hide any download links very well deep down in the site... I'll
    > keep searching..


    I think it's funny how some people try to find out things in very
    indirect ways. Instead of "Would you like to go the lastest James Bond
    movie with me, tonight?" a series of vague questions designed to elicit
    information about whether the person is likely to enjoy a James Bond
    movie, of course without any mention of James Bond, and whether the
    person has any other plans for the evening, so on. Huh, says I.

    <url: http://www.google.com/search?q=latest+draft+of+c%2B%2B+standard>

    Click on second main hit, where it says "ISO" in the title, then look
    one or two paragraphs down on that page.


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 21, 2008
    #11
  12. Lars Uffmann

    Lars Uffmann Guest

    Alf P. Steinbach wrote:
    >> I am still having a hard time finding a pdf of the latest draft -
    >> C++98 is what I need I guess - and the committee seems to be going out
    >> of its way to hide any download links very well deep down in the
    >> site... I'll keep searching..

    > I think it's funny how some people try to find out things in very
    > indirect ways. [..]


    Thanks, but I meant what I said - I found it eventually :)
    It was hard anyways, when you don't know that it's ISO 14882, and when
    you expect a link to the document to contain the words "c++ standard" at
    least :)

    Best Regards,

    Lars
     
    Lars Uffmann, Jan 21, 2008
    #12
  13. Lars Uffmann:


    > Argl... That license agreement says "single computer only"... I guess
    > I'll go for a printed book version :) Amazon, here I come...



    I agree with Alf; I had a hard-copy of the Standard one time, it was a
    mammoth of a thing and I could find no use for it. I threw it in the thrash
    eventually.

    You can always just type "14882:2003" into Google and hit "I'm Feeling
    Lucky", brings you to exactly what you're looking for.

    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Jan 21, 2008
    #13
  14. On 2008-01-21 05:06:41, Lars Uffmann wrote:

    > If I define a function, that shall return -1 in case of error, and a
    > positive value otherwise:
    >
    > int foo (int param) {
    > if (outOfRange (param))
    > return -1;
    >
    > return (param != 0);
    > }


    Just some food for thought... while this may "work", it may not always work
    as intended :)

    Looks to me as if this is a candidate for either an enum return value or an
    exception thrown.

    Gerhard
     
    Gerhard Fiedler, Jan 21, 2008
    #14
  15. Sebastian Redl wrote:
    > However, the drafts of the standard are freely available


    If I'm not completely mistaken, someone has even made man pages from
    the C++ standard draft. Any idea where those could be available?
     
    Juha Nieminen, Jan 21, 2008
    #15
  16. Re: C++ Standard hardcopy

    On 2008-01-21 09:22, Lars Uffmann wrote:
    > Sebastian Redl wrote:
    >> Lars Uffmann wrote:
    >>
    >>> Ian Collins wrote:

    >> http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS/ISO/IEC 14882-2003
    >>> Hmm - so the standard for THE programming language (imho) is not freely
    >>> available?

    >>
    >> That's correct. ISO makes some money by selling the standard documents.

    >
    > Okay, I guess I don't mind... Thanks for the hint with the drafts.
    > As for a hardcopy of the standard - all I seem to be able to find is
    > http://www.amazon.com/C++-Standard-...bs_sr_1?ie=UTF8&s=books&qid=1200903548&sr=1-1
    > - is there any other book that I've been overlooking?


    That book is the latest version of the C++ standard. There might be some
    other version of it (like paperback) but I doubt it.

    --
    Erik Wikström
     
    Erik Wikström, Jan 21, 2008
    #16
  17. On 2008-01-21 13:13, Gerhard Fiedler wrote:
    > On 2008-01-21 05:06:41, Lars Uffmann wrote:
    >
    >> If I define a function, that shall return -1 in case of error, and a
    >> positive value otherwise:
    >>
    >> int foo (int param) {
    >> if (outOfRange (param))
    >> return -1;
    >>
    >> return (param != 0);
    >> }

    >
    > Just some food for thought... while this may "work", it may not always work
    > as intended :)
    >
    > Looks to me as if this is a candidate for either an enum return value or an
    > exception thrown.


    Or at the very least using code that clearly states the intent:

    int foo(int param) {
    if (outOfRange(param))
    return -1;
    else if (param != 0)
    return 1;
    }

    --
    Erik Wikström
     
    Erik Wikström, Jan 21, 2008
    #17
  18. Erik Wikström wrote:
    > int foo(int param) {
    > if (outOfRange(param))
    > return -1;
    > else if (param != 0)
    > return 1;
    > }


    Not all paths return a value.
     
    Juha Nieminen, Jan 21, 2008
    #18
  19. On 2008-01-21 23:06, Juha Nieminen wrote:
    > Erik Wikström wrote:
    >> int foo(int param) {
    >> if (outOfRange(param))
    >> return -1;
    >> else if (param != 0)
    >> return 1;
    >> }

    >
    > Not all paths return a value.


    Ooops. Forgot

    else
    return 0;

    --
    Erik Wikström
     
    Erik Wikström, Jan 21, 2008
    #19
  20. Lars Uffmann

    Lars Uffmann Guest

    Erik Wikström wrote:
    > Or at the very least using code that clearly states the intent:
    >
    > int foo(int param) {
    > if (outOfRange(param))
    > return -1;
    > else if (param != 0)
    > return 1;
    > }


    Yes, I could do that - I was hoping to save some computing time on
    avoiding the extra if-clause for param != 0 - just return the output of
    that. And as Sebastian pointed out, in the standard, boolean true is
    promoted to 1 - so I'm safe ;)

    Best Regards,

    Lars
     
    Lars Uffmann, Jan 23, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    605
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    437
    Chris Torek
    Aug 7, 2004
  3. moosdau
    Replies:
    6
    Views:
    373
    Thad Smith
    Dec 29, 2005
  4. Jon Slaughter
    Replies:
    4
    Views:
    460
    Jon Slaughter
    Oct 26, 2005
  5. bdb112
    Replies:
    45
    Views:
    1,373
    jazbees
    Apr 29, 2009
Loading...

Share This Page