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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

C++17 creates a practical use of the backward array index operator

107 点作者 nikbackm大约 2 年前

10 条评论

omoikane大约 2 年前
Backward array index always had a use in the code golf context, like this:<p><pre><code> int a[3] = {0, 1, 2}; int *p = a; int **q = &amp;p; int x = (*q)[1]; &#x2F;&#x2F; Read a[1] int y = 1[*q]; &#x2F;&#x2F; Same, but saves 2 bytes </code></pre> Code golf considerations aren&#x27;t always related to practicality, of course.
评论 #35431866 未加载
lvkv大约 2 年前
I’ve always thought of the array index operator:<p><pre><code> a[index] </code></pre> as syntactic sugar for the pointer arithmetic:<p><pre><code> *(a + index) </code></pre> From this point of view, the existence of a “backwards” index operator makes sense; the arithmetic evaluates to the same address.
评论 #35431268 未加载
评论 #35433892 未加载
评论 #35431033 未加载
devnull3大约 2 年前
&gt; Astound your friends! Confuse your enemies!<p>... more like your friends will curse you and your enemies will watch with glee that you are using C++
olliej大约 2 年前
What this is talking about is the behavior of [] in C and C++. In these<p><pre><code> x[y] </code></pre> and<p><pre><code> y[x] </code></pre> are the same. I was first introduced to this by a friend of mine many many years ago (because I&#x27;m an old) as<p><pre><code> for (i = 0; etc) putc(i[&quot;hello world&quot;]) </code></pre> or similar nonsense.<p>The C++ change that makes this matter is that apparently pre c++17 doesn&#x27;t enforce sequencing such that expression1[expression2] doesn&#x27;t require expression1 be evaluated before expression2. C++17 does actually fix the sequencing to be left to right, so now expression1[expression2] will always evaluate as expression1;expression2 and expression2[expression1] will always evaluated as expression2;expression1 without depending on UB.
评论 #35436852 未加载
评论 #35430721 未加载
TylerGlaiel大约 2 年前
please... just explicitly calculate index() first on its own line if the order matters like this...
评论 #35436375 未加载
xorvoid大约 2 年前
Oh C++… The level of excitement over pointless triviality you generate never ceases to amaze me.<p>Some nerds somewhere are getting all giddy about this silliness when it would just be objectively better to not have this silly quirk in the first place and to write it like a sane person who recognizes the great social benefits of maximizing understanding:<p>auto idx = index(); return p[idx];<p>Clever generally just means “bad”. Why people get so excited about it mystifies me…
bfrog大约 2 年前
Ah yes, another rule to try and remember while writing a reviewing c++
评论 #35433905 未加载
评论 #35435974 未加载
gumby大约 2 年前
If you care, just use the + operator which is unambiguous.
cornstalks大约 2 年前
&gt; <i>Starting in C++17, a[b] always evaluates a before evaluating b.</i><p>Okay, I&#x27;ll bite. Why did C++17 specify this?
评论 #35430734 未加载
评论 #35432249 未加载
评论 #35436939 未加载
tpoacher大约 2 年前
Before reading the article, I thought he was talking about negative indices.<p>I was reading K&amp;R the other day and spotted the bit where they mention c supports negative indices and gave an example. My mind was blown.