Type of comparisons and conditionals

  • Thread starter Frederick Gotham
  • Start date
F

Frederick Gotham

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).
 
R

Rolf Magnus

Frederick said:
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.
 
J

Jerry Coffin

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."
 
F

Frederick Gotham

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

Victor Bazarov

Frederick said:
Jerry Coffin posted:




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
 
R

red floyd

Frederick said:
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.
 
J

Jerry Coffin

[ ... ]
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.
 
J

Jack Klein

Jerry Coffin posted:




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

Jim Langston

Frederick Gotham said:
Jerry Coffin posted:




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!";
 
J

Jim Langston

Jim Langston said:
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)
 
R

Rolf Magnus

Jim said:
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.
 
V

Victor Bazarov

Rolf said:
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
 
J

Jim Langston

Frederick Gotham said:
Jim Langston posted:



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?
 
R

Rolf Magnus

Victor said:
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.
 
J

joosteto

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)
 
J

Jim Langston

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

Jerry Coffin

[ ... ]
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."
 

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top