比较和条件的类型 [英] Type of comparisons and conditionals
问题描述
如果我们有以下表达式:
2< 1
表达式的类型是什么,2< 1,或C ++中的任何其他表达式
涉及比较或反转?更多例子:
4 == 2
!3
我有一个非常好的想法,它是bool,虽然我想确保它是
而不是int(因为它在C中)。
-
Frederick Gotham
Frederick Gotham写道:
>
如果我们有以下表达式:
2< 1
表达式的类型是什么,2<
C ++中的1或任何其他表达式涉及比较或反转?更多例子:
4 == 2
!3
我有一个非常好的想法,它是bool,虽然我想确保它是
而不是int(因为它在C中)。
你是对的。类型是bool。
在文章< al ******************* @ news .indigo.ie>, fg*******@SPAM.com
说...
>
如果我们有以下表达式:
2< 1
表达式的类型是什么,2< 1,或C ++中的任何其他表达式
涉及比较或反转?
#include< iostream>
void f(bool){
std: :cout<< 类型是bool。;
}
void f(int){
std :: cout< < Type is int。;
}
int main(){
f(2< 1);
}
任何运行正常的C ++编译器都会打印Type is bool。
- -
后来,
杰瑞。
宇宙是自己想象的虚构。
Jerry Coffin发布:
#include< iostream>
void f(bool){
std :: cout<< 类型是bool。;
}
void f(int){
std :: cout< < Type is int。;
}
int main(){
f(2< 1); < br $>
}
任何运行正常的C ++编译器都会打印Type is bool。
是的,我在发布之前自己测试过...但我想要更多的保证。
我相信我的编译器一些测试,但不适用于其他测试。
考虑我是否想测试以下两个对象是否在内存中顺序连续是否是
(并且没有任何填充)之间):
加倍a,b;
if(1 ==& b - & a)cout<< 是的,它们是连续的连续的!;
我会天真地认为,只是因为我的编译器这样做了
方式,这就是标准所说必须要做的事情。
-
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).
--
Frederick Gotham
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.
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.
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
这篇关于比较和条件的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!