473,396 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jan 21 '08 #1
11 7144
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
Jan 21 '08 #2
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
Jan 21 '08 #3
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/RecordDetai...IEC+14882-2003

--
Ian Collins.
Jan 21 '08 #4
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
Jan 21 '08 #5
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
Jan 21 '08 #6
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?
Jan 21 '08 #7
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
Jan 21 '08 #8
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.
Jan 21 '08 #9
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
Jan 21 '08 #10
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
Jan 23 '08 #11
On 23 jan, 14:34, Lars Uffmann <a...@nurfuerspam.dewrote:
>
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
Jan 23 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Charles Crume | last post by:
Hello all; I have used dBASE, and other computer languages/databases, for years. They all have a logical field type. However, the version of MySQL used by the ISP hosting my site does not...
48
by: Skybuck Flying | last post by:
Hi, I came across this C code which I wanted to understand etc it looked like this: if (-1) etc It made me wonder what the result would be... true or false ? In C and Delphi
7
by: Abubakar | last post by:
Hi, I want to know when was the True and False first included in Basic llanguage? Was it the work of Microsoft or some other company? And who decided the value of true to be -1? Regards, ...
6
by: moosdau | last post by:
it's likely that the compiler could only promise returning a non-zero value in my impression, but it did return 1 every time, like the code below: int a=3,b=8; printf("%d\n",a<b); will it ...
43
by: sinister | last post by:
Is MS Access a true RDBMS?
30
by: Jason | last post by:
I am fairly new to ASP--I have been using it about 2 months. I did these tests (below), and it doesn't make sense to me. False is equal to 0, and that's fine. True should be equal to 1, but it's...
2
by: Emmanuel Deloget | last post by:
Hello, I'm trying to find a (sfinae powered) way to verify if a particular type declares a subtype (either using typedef or by declaring a subclass). To be more concrete, let's say that I'm...
11
by: Dominic Vella | last post by:
I am using MS-Access2000. I can't seem to set the default values for Logical type fields. I start with Dim dbsTmp As Object ' I think it's DAO.Database Set dbsTmp =...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectóplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.