By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

Type of comparisons and conditionals

P: n/a

If we have the following expression:

2 < 1

What is the type of the expression, "2 < 1", or any other expression in C++
which involves comparison or inversion? More examples:

4 == 2

!3

I've a pretty good idea that it's bool, although I want to make sure it is
not int (as it is in C).

--

Frederick Gotham
Jul 7 '06 #1
Share this Question
Share on Google+
21 Replies


P: n/a
Frederick Gotham wrote:
>
If we have the following expression:

2 < 1

What is the type of the expression, "2 < 1", or any other expression in
C++ which involves comparison or inversion? More examples:

4 == 2

!3

I've a pretty good idea that it's bool, although I want to make sure it is
not int (as it is in C).
You are right. The type is bool.

Jul 7 '06 #2

P: n/a
In article <al*******************@news.indigo.ie>, fg*******@SPAM.com
says...
>
If we have the following expression:

2 < 1

What is the type of the expression, "2 < 1", or any other expression in C++
which involves comparison or inversion?
#include <iostream>

void f(bool) {
std::cout << "Type is bool.";
}

void f(int) {
std::cout << "Type is int.";
}

int main() {
f(2<1);
}

With any properly-functioning C++ compiler will print "Type is bool."

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 7 '06 #3

P: n/a
Jerry Coffin posted:

#include <iostream>

void f(bool) {
std::cout << "Type is bool.";
}

void f(int) {
std::cout << "Type is int.";
}

int main() {
f(2<1);
}

With any properly-functioning C++ compiler will print "Type is bool."

Yes, I tested that myself before posting... but I wanted more assurance.

I trust my compiler for some tests, but not for others.

Consider if I wanted to test whether the following two objects are
sequentially contiguous in memory (and without any padding in between):

double a, b;

if(1 == &b - &a) cout << "Yes, they're sequentially contiguous!";

I would be naive to presume that, just because my compiler does it that
way, that that's how the Standard says it has to be done.

--

Frederick Gotham
Jul 7 '06 #4

P: n/a
Frederick Gotham wrote:
Jerry Coffin posted:

>#include <iostream>

void f(bool) {
std::cout << "Type is bool.";
}

void f(int) {
std::cout << "Type is int.";
}

int main() {
f(2<1);
}

With any properly-functioning C++ compiler will print "Type is bool."


Yes, I tested that myself before posting... but I wanted more
assurance.
More than from a copy of the Standard? Do you own one? If not, get one.
If yes, why ask here when you can read there?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 7 '06 #5

P: n/a
Frederick Gotham wrote:
>
Consider if I wanted to test whether the following two objects are
sequentially contiguous in memory (and without any padding in between):

double a, b;

if(1 == &b - &a) cout << "Yes, they're sequentially contiguous!";

I would be naive to presume that, just because my compiler does it that
way, that that's how the Standard says it has to be done.
I believe that the Standard says that the result of subtracting two
addresses is undefined unless they point to parts of the same array.
Jul 7 '06 #6

P: n/a
In article <F9*******************@news.indigo.ie>, fg*******@SPAM.com
says...

[ ... ]
With any properly-functioning C++ compiler will print "Type is bool."


Yes, I tested that myself before posting... but I wanted more assurance.

I trust my compiler for some tests, but not for others.
[ ... ]
I would be naive to presume that, just because my compiler does it that
way, that that's how the Standard says it has to be done.
That's why I told you the result with any properly-functioning
compiler instead of advising you to run it as a test.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 7 '06 #7

P: n/a
On Fri, 07 Jul 2006 16:42:13 GMT, Frederick Gotham
<fg*******@SPAM.comwrote in comp.lang.c++:
Jerry Coffin posted:

#include <iostream>

void f(bool) {
std::cout << "Type is bool.";
}

void f(int) {
std::cout << "Type is int.";
}

int main() {
f(2<1);
}

With any properly-functioning C++ compiler will print "Type is bool."


Yes, I tested that myself before posting... but I wanted more assurance.

I trust my compiler for some tests, but not for others.

Consider if I wanted to test whether the following two objects are
sequentially contiguous in memory (and without any padding in between):

double a, b;

if(1 == &b - &a) cout << "Yes, they're sequentially contiguous!";
The comparison in this particular case is free to yield anything at
all, since the subtraction is undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 7 '06 #8

P: n/a
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:F9*******************@news.indigo.ie...
Jerry Coffin posted:

>#include <iostream>

void f(bool) {
std::cout << "Type is bool.";
}

void f(int) {
std::cout << "Type is int.";
}

int main() {
f(2<1);
}

With any properly-functioning C++ compiler will print "Type is bool."


Yes, I tested that myself before posting... but I wanted more assurance.

I trust my compiler for some tests, but not for others.

Consider if I wanted to test whether the following two objects are
sequentially contiguous in memory (and without any padding in between):

double a, b;

if(1 == &b - &a) cout << "Yes, they're sequentially contiguous!";

I would be naive to presume that, just because my compiler does it that
way, that that's how the Standard says it has to be done.
Subtracting or adding two pointers is undefined. Better would be
if ( &a + sizeof( double ) == &b ) std::cout << "Yes, they're sequentially
contiguous!";
Jul 13 '06 #9

P: n/a
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:7G**********@fe04.lga...
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:F9*******************@news.indigo.ie...
>Jerry Coffin posted:

>>#include <iostream>

void f(bool) {
std::cout << "Type is bool.";
}

void f(int) {
std::cout << "Type is int.";
}

int main() {
f(2<1);
}

With any properly-functioning C++ compiler will print "Type is bool."


Yes, I tested that myself before posting... but I wanted more assurance.

I trust my compiler for some tests, but not for others.

Consider if I wanted to test whether the following two objects are
sequentially contiguous in memory (and without any padding in between):

double a, b;

if(1 == &b - &a) cout << "Yes, they're sequentially contiguous!";

I would be naive to presume that, just because my compiler does it that
way, that that's how the Standard says it has to be done.

Subtracting or adding two pointers is undefined. Better would be
if ( &a + sizeof( double ) == &b ) std::cout << "Yes, they're sequentially
contiguous!";
Incidently, read somewhere that it is defined (subtracting two pointers) if
they are pointers to the same object. So if it's well defined or not is a
little ambiguous to me here, but better to use the pointer addition as it
should be better defined. (There may be cases where it's undefined, such as
when the addion points past the page frame)
Jul 13 '06 #10

P: n/a
Jim Langston posted:
Subtracting or adding two pointers is undefined.

Open up "stddef.h", you'll see the definition of:

ptrdiff_t
--

Frederick Gotham
Jul 13 '06 #11

P: n/a
Jim Langston wrote:
>>Consider if I wanted to test whether the following two objects are
sequentially contiguous in memory (and without any padding in between):

double a, b;

if(1 == &b - &a) cout << "Yes, they're sequentially contiguous!";

I would be naive to presume that, just because my compiler does it that
way, that that's how the Standard says it has to be done.

Subtracting or adding two pointers is undefined. Better would be
if ( &a + sizeof( double ) == &b ) std::cout << "Yes, they're
sequentially contiguous!";

Incidently, read somewhere that it is defined (subtracting two pointers)
if they are pointers to the same object.
If they point to the same object, they are equal and the difference is 0.
Subtracting two pointers (or actually any kind of pointer arithmetic) is
only defined if they both point to elements of the same array or one
element past it. So the above would be undefined because a and b are not
elements of one array.

Jul 13 '06 #12

P: n/a
Rolf Magnus wrote:
Jim Langston wrote:
>>>Consider if I wanted to test whether the following two objects are
sequentially contiguous in memory (and without any padding in
between):

double a, b;

if(1 == &b - &a) cout << "Yes, they're sequentially
contiguous!";

I would be naive to presume that, just because my compiler does it
that way, that that's how the Standard says it has to be done.

Subtracting or adding two pointers is undefined. Better would be
if ( &a + sizeof( double ) == &b ) std::cout << "Yes, they're
sequentially contiguous!";

Incidently, read somewhere that it is defined (subtracting two
pointers) if they are pointers to the same object.

If they point to the same object, they are equal and the difference
is 0. Subtracting two pointers (or actually any kind of pointer
arithmetic) is only defined if they both point to elements of the
same array or one element past it. So the above would be undefined
because a and b are not elements of one array.
Right. I thought it's also OK to subtract two pointers if they point to
objects which are subobjects of the same object, but I just checked and
didn't find that. Strange.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 13 '06 #13

P: n/a
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:gV*******************@news.indigo.ie...
Jim Langston posted:
>Subtracting or adding two pointers is undefined.


Open up "stddef.h", you'll see the definition of:

ptrdiff_t
typedef _W64 int ptrdiff_t;

What does that have to do with anything?
Jul 13 '06 #14

P: n/a
Victor Bazarov wrote:
Rolf Magnus wrote:
>Jim Langston wrote:
>>>>Consider if I wanted to test whether the following two objects are
sequentially contiguous in memory (and without any padding in
between):
>
double a, b;
>
if(1 == &b - &a) cout << "Yes, they're sequentially
contiguous!";
>
I would be naive to presume that, just because my compiler does it
that way, that that's how the Standard says it has to be done.

Subtracting or adding two pointers is undefined. Better would be
if ( &a + sizeof( double ) == &b ) std::cout << "Yes, they're
sequentially contiguous!";

Incidently, read somewhere that it is defined (subtracting two
pointers) if they are pointers to the same object.

If they point to the same object, they are equal and the difference
is 0. Subtracting two pointers (or actually any kind of pointer
arithmetic) is only defined if they both point to elements of the
same array or one element past it. So the above would be undefined
because a and b are not elements of one array.

Right. I thought it's also OK to subtract two pointers if they point to
objects which are subobjects of the same object, but I just checked and
didn't find that. Strange.
Well, wasn't there some clause that says that an object can be treated like
an array of char? I guess that would mean that you would at least be
allowed to cast the pointers to the subobjects to char* and then compare
them.

Jul 13 '06 #15

P: n/a
Jim Langston posted:
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:gV*******************@news.indigo.ie...
>Jim Langston posted:
>>Subtracting or adding two pointers is undefined.


Open up "stddef.h", you'll see the definition of:

ptrdiff_t

typedef _W64 int ptrdiff_t;

What does that have to do with anything?

What do you suppose its purpose is?
--

Frederick Gotham
Jul 13 '06 #16

P: n/a
Subtracting or adding two pointers is undefined. Better would be
if ( &a + sizeof( double ) == &b ) std::cout << "Yes, they're sequentially
contiguous!";
Why the sizeof(double)? That should be "1" (at least my g++ thinks so).
Or else, if you insist, "(char *)&a +sizeof(double) == (char *)&b":

double d[10];
double &a=d[0];
double &b=d[1];

if ( &a + sizeof( double ) == &b ) {
std::cout << "Yes, they're sequentially"<<std::endl;
};
if ( &a + 1 == &b ) {
std::cout << "They are half sequentially"<<std::endl;
}
if( (char *)&a +sizeof(double) == (char *)&b){
std::cout << "char * cast differs sizeof(double)"<<endl;
}

this prints (g++ 4.0.3):

They are half sequentially
char * cast differs sizeof(double)

Jul 13 '06 #17

P: n/a
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:FG*******************@news.indigo.ie...
Jim Langston posted:
>"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:gV*******************@news.indigo.ie...
>>Jim Langston posted:

Subtracting or adding two pointers is undefined.
Open up "stddef.h", you'll see the definition of:

ptrdiff_t

typedef _W64 int ptrdiff_t;

What does that have to do with anything?


What do you suppose its purpose is?
Did you read my response to my own message and the follow up?
Jul 13 '06 #18

P: n/a
<jo******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>Subtracting or adding two pointers is undefined. Better would be
if ( &a + sizeof( double ) == &b ) std::cout << "Yes, they're
sequentially
contiguous!";

Why the sizeof(double)? That should be "1" (at least my g++ thinks so).
Or else, if you insist, "(char *)&a +sizeof(double) == (char *)&b":

double d[10];
double &a=d[0];
double &b=d[1];

if ( &a + sizeof( double ) == &b ) {
std::cout << "Yes, they're sequentially"<<std::endl;
};
if ( &a + 1 == &b ) {
std::cout << "They are half sequentially"<<std::endl;
}
if( (char *)&a +sizeof(double) == (char *)&b){
std::cout << "char * cast differs sizeof(double)"<<endl;
}

this prints (g++ 4.0.3):

They are half sequentially
char * cast differs sizeof(double)
Doh! You are absolutely right. I usually only do pointer math on char
arrays.
Jul 14 '06 #19

P: n/a
In article <e9**********@news.datemas.de>, v.********@comAcast.net
says...

[ ... ]
Right. I thought it's also OK to subtract two pointers if they point to
objects which are subobjects of the same object, but I just checked and
didn't find that. Strange.
$5.7/6: "Unless both pointers point to elements of the same array
object, or one past the last element of the array object, the
behavior is undefined."

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 16 '06 #20

P: n/a
Jerry Coffin wrote:
In article <e9**********@news.datemas.de>, v.********@comAcast.net
says...

[ ... ]
>Right. I thought it's also OK to subtract two pointers if they
point to objects which are subobjects of the same object, but I just
checked and didn't find that. Strange.

$5.7/6: "Unless both pointers point to elements of the same array
object, or one past the last element of the array object, the
behavior is undefined."
Yes, and?
Jul 17 '06 #21

P: n/a
In article <8I******************************@comcast.com>,
v.********@comAcast.net says...
Jerry Coffin wrote:
In article <e9**********@news.datemas.de>, v.********@comAcast.net
says...

[ ... ]
Right. I thought it's also OK to subtract two pointers if they
point to objects which are subobjects of the same object, but I just
checked and didn't find that. Strange.
$5.7/6: "Unless both pointers point to elements of the same array
object, or one past the last element of the array object, the
behavior is undefined."

Yes, and?
And therefore, if they are subobjects of the same object (rather than
elements of the same array) the behavior is undefined.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 17 '06 #22

This discussion thread is closed

Replies have been disabled for this discussion.