TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Humans should think of sizeof() as a function, says Linus Torvalds

111 点作者 arjun024将近 10 年前

16 条评论

krylon将近 10 年前
IIRC, if the argument to sizeof is a type, the parentheses are mandatory, anyway, so using it like it was a function is more consistent.<p>I think the reason it is not a function from the standard&#x27;s point of view is that C does not have any builtin functions (unless my memory totally fails me in this case), all functions have to be either defined locally or #included.
评论 #9629786 未加载
评论 #9629915 未加载
评论 #9629897 未加载
评论 #9629890 未加载
Udo将近 10 年前
I&#x27;d like to confess that I&#x27;m one of the return() people, and I also do if() even in cases where the language doesn&#x27;t require it. This behavior isn&#x27;t borne out of confusion about what is and what isn&#x27;t a special language construct. Rather, it helps me prop up the illusion that programming is about using a few simple primitives instead of being the chain of compiler directives it actually entails - it&#x27;s an esthetic choice if not always a logical one.<p>The thought that if() <i>could</i> just be a lazily-evaluated function taking a code block argument, and that return() <i>could</i> be a way of marking the end result of an expression somehow pleases me.<p>I think the different expectations about sizeof() come from the artificial distinction between operators and functions, the implication being that in a compiled language the sizeof operator would be a compile-time construct, or barring that, at least a behavior of the type system. On the other hand, there are tons of compiler intrinsics in C&#x2F;C++ that look exactly like functions but aren&#x27;t.
评论 #9629778 未加载
评论 #9629779 未加载
Negative1将近 10 年前
I respect Linus greatly but the way he to talks to his fellow &#x27;humans&#x27; is insanely confrontational, rude and disrespectful. It overshadows his arguments (which are usually very good) and throws people on the defensive. I&#x27;m grateful for all he has accomplished and shared with us but I imagine working with him is &#x27;hell&#x27;.
评论 #9630644 未加载
评论 #9630101 未加载
评论 #9630188 未加载
评论 #9630104 未加载
gpvos将近 10 年前
I used to write<p><pre><code> return (0); </code></pre> I.e., with a space. I&#x27;m not sure if someone ever told or recommended me to do this, but the reason I did this was consistency with other C statements, because <i>all</i> C statements that take some kind of expression as a parameter (if, for, while) require it to be surrounded by parentheses. So it seemed logical to me to do the same with return. I&#x27;ve stopped doing it now, although some of the old code still lives.<p>I&#x27;ve seen code by others where there was always a space between the function name and its arguments. <i>That</i> was ugly, and really confused a function call with a statement.
评论 #9630229 未加载
tvb将近 10 年前
Makes a good C interview question:<p><pre><code> char c; short h; int i; char *s; 1 = sizeof (char) 2 = sizeof (short) 4 = sizeof (int) 4 = sizeof (float) 8 = sizeof (double) 1 = sizeof (c) 2 = sizeof (h) 4 = sizeof (i) 4 = sizeof (s) 4 = sizeof &amp;c 1 = sizeof c 2 = sizeof h 4 = sizeof i 4 = sizeof s 1 = sizeof *s 4 = sizeof &amp;main 4 = sizeof (sizeof (i)) 4 = sizeof (sizeof i) 4 = sizeof sizeof i 4 = sizeof sizeof sizeof i </code></pre> from: <a href="http:&#x2F;&#x2F;leapsecond.com&#x2F;tools&#x2F;size1.c" rel="nofollow">http:&#x2F;&#x2F;leapsecond.com&#x2F;tools&#x2F;size1.c</a>
moonbug将近 10 年前
&quot;Quite frankly, if you do this, you should be shot. &quot;<p>Ah, yes. No comment from Torvalds is complete without a gratuitous ad hominem.
评论 #9630310 未加载
评论 #9630126 未加载
评论 #9630192 未加载
Chathamization将近 10 年前
I first learned that sizeof was an operator when I was confused about how it dealt with arrays compared to functions. After I learned it was an operator and not a function things made sense and were (relatively) consistent again. Several commentators here have given other examples where you will be completely thrown off if you believe that &#x27;&quot;sizeof()&quot; really <i>is</i> a function.&#x27;<p>The problem with &quot;lies to children&quot; is that they can pile up to the point where people end up becoming completely confused about what&#x27;s actually happening. As the internal inconsistencies mount, the &quot;simple&quot; lie starts to become a lot more complex than the &quot;complicated&quot; truth.
nmj将近 10 年前
&gt; Here&#x27;s an example of a really bad use of &quot;sizeof&quot; that doesn&#x27;t have the parenthesis around the argument: sizeof(*p)-&gt;member. Quite frankly, if you do this, you should be shot.<p>Good ol&#x27; Linus
评论 #9630106 未加载
mburns将近 10 年前
(2012)
mattkrea将近 10 年前
Just as annoying as people not using parens with &quot;typeof()&quot; in JavaScript.
tormeh将近 10 年前
Why is this important?
wz1000将近 10 年前
&gt; &quot;return()&quot; is in no way a function.<p>Ah, but in Haskell, `return` is a function, though it shares only a few similarities with with its counterpart in C-like languages.<p>However, when using continuations or a continuation passing style, the continuation is a function that behaves almost exactly like traditional return when called!<p><pre><code> (define (multiply x y return) (return (* x y)))</code></pre>
评论 #9630519 未加载
progrn将近 10 年前
But, I cannot get a function pointer to it, right?
评论 #9629848 未加载
评论 #9629836 未加载
soup10将近 10 年前
What kind of monsters don&#x27;t use parentheses on sizeof. Is it the same guys that put { on new lines and have giant comment templates for every one line function.
评论 #9629902 未加载
评论 #9629828 未加载
lazzlazzlazz将近 10 年前
Who the hell doesn&#x27;t think of `sizeof` as a function?
评论 #9629796 未加载
评论 #9629727 未加载
评论 #9629758 未加载
评论 #9629733 未加载
kazinator将近 10 年前
Yes, return can be a function. Torvalds hasn&#x27;t heard of continuations, obviously, where we &quot;return&quot; from a function by invoking a continuation.<p>We can justify writing return (expr); using the same arguments that justify the sizeof (expr) convention.<p>The thing is that <i>in C</i>, return isn&#x27;t a function; there are no continuations.<p>Similarly, sizeof is an operator, which doesn&#x27;t reduce its argument expression to a value.<p>If we are going to make coding conventions based on pretending that C is a different language in which sizeof is a function, then pretending return is a function is also fair game.
评论 #9630542 未加载
评论 #9634285 未加载
评论 #9630577 未加载