Definition of logical true in C++?



 DEVELOP > c-Plus-Plus > Definition of logical true in C++?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Lars Uffmann"
Date: 21 Jan 2008 01:06:41 AM
Object: Definition of logical true in C++?
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
.

User: "Sebastian Redl"

Title: Re: Definition of logical true in C++? 21 Jan 2008 01:32:32 AM
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
.
User: "Lars Uffmann"

Title: Re: Definition of logical true in C++? 21 Jan 2008 01:45:26 AM
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
.
User: "Ian Collins"

Title: Re: Definition of logical true in C++? 21 Jan 2008 01:52:57 AM
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%2FISO%2FIEC+14882-2003
--
Ian Collins.
.
User: "Lars Uffmann"

Title: Re: Definition of logical true in C++? 21 Jan 2008 02:00:52 AM
Ian Collins wrote:

http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+14882-2003

Hmm - so the standard for THE programming language (imho) is not freely
available?
Not that I mind ordering a copy... just wondrin'... *headscratch*
Thank you :)
Lars
.
User: "Sebastian Redl"

Title: Re: Definition of logical true in C++? 21 Jan 2008 02:04:11 AM
Lars Uffmann wrote:

Ian Collins wrote:


http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+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
.
User: "Juha Nieminen"

Title: Re: Definition of logical true in C++? 21 Jan 2008 09:53:01 AM
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?
.

User: "Lars Uffmann"

Title: C++ Standard hardcopy (was: Re: Definition of logical true in C++?) 21 Jan 2008 02:22:01 AM
Sebastian Redl wrote:

Lars Uffmann wrote:

Ian Collins wrote:

http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+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%2B%2B-Standard-Incorporating-Technical-Corrigendum/dp/0470846747/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1200903548&sr=1-1
- is there any other book that I've been overlooking?
Thanks!
Lars
.
User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: C++ Standard hardcopy 21 Jan 2008 11:50:00 AM
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%2FISO%2FIEC+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%2B%2B-Standard-Incorporating-Technical-Corrigendum/dp/0470846747/ref=pd_bbs_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
.



User: "Lars Uffmann"

Title: Re: Definition of logical true in C++? 21 Jan 2008 02:11:00 AM
Lars Uffmann wrote:

Ian Collins wrote:

http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+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...
Regards,
Lars
.
User: "Alf P. Steinbach"

Title: Re: Definition of logical true in C++? 21 Jan 2008 02:33:06 AM
* Lars Uffmann:

Lars Uffmann wrote:

Ian Collins wrote:

http://webstore.ansi.org/RecordDetail.aspx?sku=INCITS%2FISO%2FIEC+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?
.
User: "Lars Uffmann"

Title: Re: Definition of logical true in C++? 21 Jan 2008 02:58:02 AM
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
.
User: "Alf P. Steinbach"

Title: Re: Definition of logical true in C++? 21 Jan 2008 03:38:08 AM
* 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?
.
User: "Lars Uffmann"

Title: Re: Definition of logical true in C++? 21 Jan 2008 05:29:12 AM
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
.




User: "=?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?="

Title: Re: Definition of logical true in C++? 21 Jan 2008 05:41:57 AM
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
.






User: "Gerhard Fiedler"

Title: Re: Definition of logical true in C++? 21 Jan 2008 06:13:39 AM
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
.
User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: Definition of logical true in C++? 21 Jan 2008 11:57:36 AM
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
.
User: "Lars Uffmann"

Title: Re: Definition of logical true in C++? 23 Jan 2008 02:46:36 AM
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
.
User: "Gerhard Fiedler"

Title: Re: Definition of logical true in C++? 23 Jan 2008 06:01:47 AM
On 2008-01-23 06:46:36, Lars Uffmann wrote:

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

else
return 0;

}


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.

I think (but don't know) that any decent compiler would optimize that away
if possible. In this case you wouldn't save any computing time. Actually,
it's quite possible that even without any optimization
return (param != 0);
and
if (param != 0)
return 1;
else
return 0;
and
return (param != 0) ? 1 : 0;
create the same assembly code on many compilers.

And as Sebastian pointed out, in the standard, boolean true is
promoted to 1 - so I'm safe ;)

Yes, but the point is that you had to resort to the newsgroup to know that.
Another programmer might have to do the same in order to understand code
that relies on this. The latter two versions above make that explicit.
Gerhard
.
User: "Lars Uffmann"

Title: Re: Definition of logical true in C++? 23 Jan 2008 07:34:41 AM
Gerhard Fiedler wrote:

And as Sebastian pointed out, in the standard, boolean true is
promoted to 1 - so I'm safe ;)


Yes, but the point is that you had to resort to the newsgroup to know that.
Another programmer might have to do the same in order to understand code
that relies on this. The latter two versions above make that explicit.

Point taken, but I'd like to know the compiler behaviour on this for
sure before I use the more self-explaining code :) Otherwise I'll just
add the appropriate comments to the code. After all, I'm not writing a
book, but trying to program fast applications ;) Self-explaining code
yes, but only as long as it doesn't affect speed in a bad way.
Best Regards,
Lars
.
User: "Bart van Ingen Schenau"

Title: Re: Definition of logical true in C++? 23 Jan 2008 09:10:30 AM
On 23 jan, 14:34, Lars Uffmann <a...@nurfuerspam.de> wrote:


Point taken, but I'd like to know the compiler behaviour on this for
sure before I use the more self-explaining code :) Otherwise I'll just
add the appropriate comments to the code. After all, I'm not writing a
book, but trying to program fast applications ;) Self-explaining code
yes, but only as long as it doesn't affect speed in a bad way.

And the best way to adversely affect the speed of your application is
to try to outsmart your compiler. :-)
Optimisers get better all the time, and they work by looking at
patterns within the (generated) code. The first patterns that get
added to an optimiser are those for idiomatic code, because those
should occur very frequently and thus can have a big impact on
optimisation.
Writing non-idiomatic code may result in missed opportunities because
the optimiser does not recognise it.


Best Regards,

Lars

Bart v Ingen Schenau
.




User: "Juha Nieminen"

Title: Re: Definition of logical true in C++? 21 Jan 2008 04:06:30 PM
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.
.
User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: Definition of logical true in C++? 21 Jan 2008 04:37:45 PM
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
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER