为什么sizeof的视为运营商? [英] Why is sizeof considered as an operator?

查看:139
本文介绍了为什么sizeof的视为运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么的sizeof 认为作为一个经营者,而不是一个功能?

Why is sizeof considered as an operator and not a function ?

什么财产是必要的资格作为操作?

What property is necessary to qualify as an operator ?

推荐答案

由于C标准是这样说的,而且它得到了投票。

Because the C standard says so, and it gets the only vote.

至于后果:


  • 的sizeof的操作数可以是裸投,的sizeof(INT),而不是一个对象前pression。

  • 括号是不必要的: int类型的;的printf(%d个\\ n,sizeof的一个); 是完全没有问题。他们经常看到的,首先因为他们需要一个类型转换前pression的一部分,其次是因为sizeof的具有非常高的precedence,所以 sizeof的A + B 是不一样的的sizeof(A + b)。但他们不是的sizeof调用的一部分,它们是操作的一部分。

  • 您不能把sizeof的地址。

  • 这位前pression这是sizeof运算的操作数是不是在运行时进行评估(的sizeof一个++ 不修改)。

  • 这位前pression这是sizeof运算的操作数可以是任意类型,除了无效或函数类型。事实上,这是种的sizeof点的。

  • The operand of sizeof can be a bare cast, sizeof (int), instead of an object expression.
  • The parentheses are unnecessary: int a; printf("%d\n", sizeof a); is perfectly fine. They're often seen, firstly because they're needed as part of a type cast expression, and secondly because sizeof has very high precedence, so sizeof a + b isn't the same as sizeof (a+b). But they aren't part of the invocation of sizeof, they're part of the operand.
  • You can't take the address of sizeof.
  • The expression which is the operand of sizeof is not evaluated at runtime (sizeof a++ does not modify a).
  • The expression which is the operand of sizeof can have any type except void, or function types. Indeed, that's kind of the point of sizeof.

一个函数将在所有这些点不同。可能有一个函数和一个一元运算符之间的其他差异,但我认为这足以说明为什么sizeof的不可能是一个函数即使是希望它是有原因的。

A function would differ on all those points. There are probably other differences between a function and a unary operator, but I think that's enough to show why sizeof could not be a function even if there was a reason to want it to be.

这篇关于为什么sizeof的视为运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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