Definition of logical true in C++?

L

Lars Uffmann

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
 
S

Sebastian Redl

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.
 
L

Lars Uffmann

Sebastian said:
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
 
S

Sebastian Redl

Lars said:
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.)
 
L

Lars Uffmann

Lars said:
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
 
A

Alf P. Steinbach

* Lars Uffmann:
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
 
L

Lars Uffmann

Alf said:
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
 
A

Alf P. Steinbach

* Lars Uffmann:
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 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+++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
 
L

Lars Uffmann

Alf said:
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
 
T

Tomás Ó hÉilidhe

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.
 
G

Gerhard Fiedler

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
 
J

Juha Nieminen

Sebastian said:
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?
 
E

Erik Wikström

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;
}
 
L

Lars Uffmann

Erik said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top