Employer looking 4 online C++ aptitude tests; recommendations?

M

Mike Wahler

Bob Hairgrove said:
Well, it does compile and run correctly under Comeau, returning 1:

Because 'true' is output as '1' (and 'false' as 0) by operator<<,
by default (via a facet of a locale). (This behavior can be changed
(with the 'boolapha' format flag or manipulator).
#include <iostream>
#include <ostream>

int main()
{
std::cout << "Result of !((1 || 0) && 0):\t";
std::cout << !((1 || 0) && 0) << std::endl;
return 0;
}

After all, bool is defined by the standard to be
"an integral type".
Question is, whether other output != 0 (e.g. 4711) should be allowed.

Does the standard specify what the integral value of "true" should be?

======================================================================
ISO/IEC 14882:1998(E)

4.5 Integral promotions

4 An rvalue of type bool can be converted to an rvalue of type int,
with false becoming zero and true becoming one.
======================================================================
-Mike
 
S

Seungbeom Kim

Bob said:
After all, bool is defined by the standard to be "an integral type".
Question is, whether other output != 0 (e.g. 4711) should be allowed.

Does the standard specify what the integral value of "true" should be?

"An rvalue of type bool can be converted to an rvalue of type int, with
false becoming zero and true becoming one." - 4.5/4
 
J

Jerry Coffin

After all, bool is defined by the standard to be "an integral type".
Question is, whether other output != 0 (e.g. 4711) should be allowed.

true converted to any other integer type becomes 1 -- but its own value
is true, not 1.
Does the standard specify what the integral value of "true" should
be?

Yes -- the integral value of true is exactly that -- true. bool is an
integer type that has exactly two values: false and true. When a bool
is converted to some other integer type, true converts to 1 and false
converts to 0. This is much like a null pointer: an integer constant
with the value 0 can be converted to a null pointer, but is not iself a
pointer at all. Likewise, a bool can convert to an int (or char, short,
etc.) but is not an int itself.
 
J

Jerry Coffin

Bob Hairgrove wrote:

[ ... ]
After all, bool is defined by the standard to be "an integral type".
Question is, whether other output != 0 (e.g. 4711) should be allowed.

true converted to any other integer type becomes 1 -- but its own value
is true, not 1.
Does the standard specify what the integral value of "true" should
be?

The standard specifies that the value of true is true. It does't _have_
an integral value; rather, it IS an integral value. Asking what the
value of true should be is roughly like asking what the value of 1
should be.
 
A

Allan W

Jerry said:
The standard specifies that the value of true is true. It does't _have_
an integral value; rather, it IS an integral value. Asking what the
value of true should be is roughly like asking what the value of 1
should be.

Web page
http://www.cogsci.princeton.edu/cgi-bin/webwn?stage=1&word=one,
defines 1 as: "the smallest whole number or a numeral representing
this number"

Similarly
http://www.cogsci.princeton.edu/cgi-bin/webwn2.0?stage=1&word=true
defines true as:
consistent with fact or reality; not false
 
K

kanze

Jerry said:
Bob Hairgrove wrote:
[ ... ]
After all, bool is defined by the standard to be "an
integral type". Question is, whether other output != 0
(e.g. 4711) should be allowed.
true converted to any other integer type becomes 1 -- but its
own value is true, not 1.
The standard specifies that the value of true is true. It
does't _have_ an integral value; rather, it IS an integral
value. Asking what the value of true should be is roughly
like asking what the value of 1 should be.

I rather agree with you, but the standard also says (§3.0./1/7):
"The representations of integral types shall define values by
use of a pure binary numeration system." Now how do you
represent "true" as a sum of powers of 2?

(Why is it that so often when reading the standard, I am
reminded of the words:
Oh, what a tangled web we weave,
When first we practice to deceive.
If they hadn't said that bool was an integer, we wouldn't have
these problems:).

And before anyone gets me wrong, I do understand the motivations
for making bool an integral type, and given everything, I think
it was a less bad decision than the alternatives. But that
doesn't change the fact that it is a deception, and that it
leads to some tangled webs.

Such is the price of backwards compatibility. And backwards
compatibility is the price of acceptance.)
 
J

Jerry Coffin

(e-mail address removed) wrote:

[ ... ]
I rather agree with you, but the standard also says (§3.0./1/7):
"The representations of integral types shall define values by
use of a pure binary numeration system." Now how do you
represent "true" as a sum of powers of 2?

Usually as either 1 or whatever value you assign to "all bits set": -1
on a two's complement machine, -0 on a 1's complement, INTMIN on a
sign/magnitude.
(Why is it that so often when reading the standard, I am
reminded of the words:
Oh, what a tangled web we weave,
When first we practice to deceive.
If they hadn't said that bool was an integer, we wouldn't have
these problems:).

And before anyone gets me wrong, I do understand the motivations
for making bool an integral type, and given everything, I think
it was a less bad decision than the alternatives. But that
doesn't change the fact that it is a deception, and that it
leads to some tangled webs.

There's also the fact that the standard is sufficiently large that all
the consequences of a decision are rarely (if ever) immediately
obvious.
Such is the price of backwards compatibility. And backwards
compatibility is the price of acceptance.)

Quite true -- Objective C (for one example) is arguably more
consistent, but while it isn't obscure by any means, it certainly has a
lot smaller market share than C++.
 
V

Vorticity Kappa

Jerry Coffin said:
(e-mail address removed) wrote:

[ ... ]
I rather agree with you, but the standard also says (§3.0./1/7):
"The representations of integral types shall define values by
use of a pure binary numeration system." Now how do you
represent "true" as a sum of powers of 2?

Usually as either 1 or whatever value you assign to "all bits set": -1
on a two's complement machine, -0 on a 1's complement, INTMIN on a
sign/magnitude.

You left out Kepler ternary . (The Russians were rumored to have built a
computer that ran in it.)

Are there non-2s-complement computers being built anywhere today?
 
J

Jerry Coffin

Vorticity Kappa wrote:

[ ... ]
You left out Kepler ternary . (The Russians were rumored to have
built a computer that ran in it.)

The standard requires a binary representation for integers. :)
Are there non-2s-complement computers being built anywhere today?

Some certainly use other representations for floating point. I'm
considerably less certain about other integer representations.
 
K

kanze

Vorticity said:
Are there non-2s-complement computers being built anywhere
today?

Unisys 2200's are still being made and marketed; it's 2200
processors are 36 bit one's complement.
 

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
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top