473,386 Members | 1,652 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,386 software developers and data experts.

Type of comparisons and conditionals


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
21 1514
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
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
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
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
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
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
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
"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
"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
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
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
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
"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
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
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
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
"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
<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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Joshua Ginsberg | last post by:
Is there any plan to include inline conditionals in Python? For example: def isNegative(x): return x < 0 ? True : False Thanks! -jag --
3
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many...
1
by: johkar | last post by:
I see some people accessing a forms' input type without converting toLowerCase() and others do. Do some browser's return type as uppercase? This document.forms.elements.type=='select-one'; ...
1
by: Paul Dale | last post by:
Hi All, I know that several of you will probably want to reply "you should write a parser", and I may. For that matter any tips on theory in that direction would be appreciated. However, if...
11
by: JohnR | last post by:
I'm trying to find a way to create a variable of a given type at runtime where I won't know the type until it actually executes. For example, dim x as object = "hi" x is declared as an object...
12
by: Dominik Werder | last post by:
Hello! When walking the DOM I check for e.g. obj.nodeName=='DIV' or similar. But this throws a warning in firefox if obj is not a HTMLDivElement. How can you test for HTMLDivElement without...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
5
by: Hul Tytus | last post by:
comp.lang.c c programs & shell conditionals How is a unix shell script made to respond to the value returned by a program compiled from c code? The shell script below is my current effort,...
8
by: Steven | last post by:
Hello, everyone! I find a version of strcpy(), I don't know why it return the unsigned char value. Can I change it into return *s1-*s2? int strcmp(const char *s1, const char *s2) { while...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.