TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

107 pointsby nikbackmabout 2 years ago

10 comments

omoikaneabout 2 years ago
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 未加载
lvkvabout 2 years ago
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 未加载
devnull3about 2 years ago
&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++
olliejabout 2 years ago
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 未加载
TylerGlaielabout 2 years ago
please... just explicitly calculate index() first on its own line if the order matters like this...
评论 #35436375 未加载
xorvoidabout 2 years ago
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…
bfrogabout 2 years ago
Ah yes, another rule to try and remember while writing a reviewing c++
评论 #35433905 未加载
评论 #35435974 未加载
gumbyabout 2 years ago
If you care, just use the + operator which is unambiguous.
cornstalksabout 2 years ago
&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 未加载
tpoacherabout 2 years ago
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.