表达的大小& sizeof of type [英] sizeof of expression & sizeof of type

查看:53
本文介绍了表达的大小& sizeof of type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么需要使用两种sizeof运算符:

* sizeof一元表达式,

* sizeof(类型名称)




他们的行为似乎没有什么不同(见下面的例子)。


------ C ++代码 - -----

#include< iostream>

使用命名空间std;


int main()

{

int x = 100;


cout<< sizeof ++ x<< ENDL; //表达式sizeof

cout<< x<< endl;


x = 100;

cout<< endl;

cout<< sizeof(++ x)<< ENDL; //输入sizeof

cout<< x<< endl;


返回0;

}

---------------- ------


------运行------


4

100


4

100


------------- ----


Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Why does one need to use two kinds of sizeof operator:
* sizeof unary-expression,
* sizeof (type-name)
?

Their behavior seem not to be different (see an example below).

------ C++ code ------
#include <iostream>
using namespace std;

int main()
{
int x = 100;

cout << sizeof ++x << endl; // expression sizeof
cout << x << endl;

x = 100;
cout << endl;
cout << sizeof (++x) << endl; // type sizeof
cout << x << endl;

return 0;
}
----------------------

------ Run ------

4
100

4
100

-----------------

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

推荐答案

Alex Vinokur写道:
Alex Vinokur wrote:
为什么需要使用两种sizeof运算符:
* sizeof unary-expression,
* sizeof(类型名称)



因为你可能想在一个值上使用它来找出它的大小,或者你想要在一个类型上使用它来查找它的大小。

他们的行为似乎没有什么不同(见下面的例子)。

------ C ++代码------
#包括< iostream>
使用命名空间std;

int main()
{/> int x = 100;

cout< < sizeof ++ x<< ENDL; //表达sizeof
cout<< x<< endl;

x = 100;
cout<< endl;
cout<< sizeof(++ x)<< ENDL; //类型sizeof


(++ x)不是类型。 int将是一种类型。

cout<< x<< endl;

返回0;
}
----------------------


4
100

4
100
Why does one need to use two kinds of sizeof operator:
* sizeof unary-expression,
* sizeof (type-name)
?
Because you might want to use it on a value to find out its size, or you
want to use it on a type to find out its size.

Their behavior seem not to be different (see an example below).

------ C++ code ------
#include <iostream>
using namespace std;

int main()
{
int x = 100;

cout << sizeof ++x << endl; // expression sizeof
cout << x << endl;

x = 100;
cout << endl;
cout << sizeof (++x) << endl; // type sizeof
(++x) is not a type. int would be a type.
cout << x << endl;

return 0;
}
----------------------

------ Run ------

4
100

4
100







" Rolf Magnus" < RA ****** @ t-online.de>在消息新闻中写道:e6 ************* @ news.t-online.com ...

"Rolf Magnus" <ra******@t-online.de> wrote in message news:e6*************@news.t-online.com...
Alex Vinokur写道:
Alex Vinokur wrote:



[snip]


[snip]


int main()
{
int x = 100;

cout<< sizeof ++ x<< ENDL; //表达sizeof
cout<< x<< endl;

x = 100;
cout<< endl;
cout<< sizeof(++ x)<< ENDL; // type sizeof

int main()
{
int x = 100;

cout << sizeof ++x << endl; // expression sizeof
cout << x << endl;

x = 100;
cout << endl;
cout << sizeof (++x) << endl; // type sizeof



(++ x)不是一种类型。 int将是一个类型。



(++x) is not a type. int would be a type.



[snip]


我认为,++ x在sizeof(++ x)中被解析为typename 。

-

Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


[snip]

I think, ++x is parsed as typename in sizeof (++x).
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Alex Vinokur写道:
Alex Vinokur wrote:
" Rolf Magnus" < RA ****** @ t-online.de>在消息新闻中写道:e6 ************* @ news.t-online.com ...
"Rolf Magnus" <ra******@t-online.de> wrote in message news:e6*************@news.t-online.com...
Alex Vinokur写道:
Alex Vinokur wrote:


[snip]


[snip]


int main()
{/> int x = 100;

cout << sizeof ++ x<< ENDL; //表达sizeof
cout<< x<< endl;

x = 100;
cout<< endl;
cout<< sizeof(++ x)<< ENDL; // type sizeof

int main()
{
int x = 100;

cout << sizeof ++x << endl; // expression sizeof
cout << x << endl;

x = 100;
cout << endl;
cout << sizeof (++x) << endl; // type sizeof



(++ x)不是一种类型。 int将是一个类型。



(++x) is not a type. int would be a type.


[snip]

我认为,++ x在sizeof(++ x)中被解析为typename。


[snip]

I think, ++x is parsed as typename in sizeof (++x).




不是它是一元表达式,因为一个主表达式也是一个表达式,而且(表达式)是一个主表达式并且很明显

++ x是一个表达式。



No it is a unary-expression because a primary-expression is also a
unary-expression and ( expression ) is a primary-expression and clearly
++x is an expression.


这篇关于表达的大小&amp; sizeof of type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆