473,225 Members | 1,294 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,225 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 1510
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.