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.

Arrays of Arrays (2009)

39 pointsby theawesomekhanover 4 years ago

5 comments

Animatsover 4 years ago
Over a decade ago I was trying to get the C++ committee to support multidimensional arrays via overloading, so we could have 2D collections. In C++, you can overload &quot;operator[]&quot;. But it can&#x27;t take more than one argument. Why?<p>Because of the C &quot;comma operator&quot;. In C, the comma operator evaluates both operands and discards the first result. It&#x27;s an ancient hack used mostly with preprocessor defines.<p>In C and C++,<p><pre><code> foo(a,b) </code></pre> is a call with two arguments. But<p><pre><code> foo[a,b] </code></pre> invokes the comma operator and presents one value to &quot;[]&quot; You have to dig through the C++ specification to find this.<p>So I asked the question, could anyone find an example of the comma operator used in C or C++ inside square brackets in any production code. I couldn&#x27;t. I got someone at IBM Almaden to search IBM&#x27;s code archive and they couldn&#x27;t. But there was the concern that somewhere, someone had used that misfeature for something.<p>So, no 2D array syntax for you.
评论 #25484587 未加载
评论 #25484812 未加载
评论 #25485761 未加载
评论 #25485432 未加载
评论 #25484078 未加载
评论 #25486879 未加载
Per_Bothnerover 4 years ago
Which is why the sane way to do it is to have the array-specifier come <i>before</i> the element type:<p><pre><code> [,][]int crazy; </code></pre> It reads &quot;2-dimensional array of array of int&quot; in L-to-R order.<p>Similarly, the type &quot;pointer-to-int&quot; should be &quot;*p&quot; or equivalent.<p>Unfortunately, history&#x2F;tradition as well parsing problems make the &quot;sane&quot; solution difficult. One solution is the Pascal approach putting the type after the variable being declared, with a colon between:<p><pre><code> crazy : [,][]int;</code></pre>
评论 #25485878 未加载
teleforceover 4 years ago
Take a look at D language library Mir for N-Dimensional array and its comparison to Numpy [1].<p>Its native performance inside GLAS implementation is something to shout about and even comparable to OpenBLAS that is currently being used by Julia and Matlab [2][3].<p>[1]<a href="http:&#x2F;&#x2F;docs.algorithm.dlang.io&#x2F;latest&#x2F;mir_ndslice.html" rel="nofollow">http:&#x2F;&#x2F;docs.algorithm.dlang.io&#x2F;latest&#x2F;mir_ndslice.html</a><p>[2]<a href="http:&#x2F;&#x2F;blog.mir.dlang.io&#x2F;glas&#x2F;benchmark&#x2F;openblas&#x2F;2016&#x2F;09&#x2F;23&#x2F;glas-gemm-benchmark.html" rel="nofollow">http:&#x2F;&#x2F;blog.mir.dlang.io&#x2F;glas&#x2F;benchmark&#x2F;openblas&#x2F;2016&#x2F;09&#x2F;23&#x2F;...</a><p>[3]<a href="http:&#x2F;&#x2F;docs.mir.dlang.io&#x2F;latest&#x2F;mir_glas.html" rel="nofollow">http:&#x2F;&#x2F;docs.mir.dlang.io&#x2F;latest&#x2F;mir_glas.html</a>
kqrover 4 years ago
This reasoning seems to me similar to how declaring a pointer in e.g. C works: many beginners write<p><pre><code> int* a, b; </code></pre> And think that this constructs two pointers-to-integer. In fact, it constructs one pointer and one plain integer. A better (equivalent) way to write it is<p><pre><code> int *a, b; </code></pre> You&#x27;re not declaring variables of type<p><pre><code> int* </code></pre> You&#x27;re saying that b is an int and<p><pre><code> *a </code></pre> Is an int, where the asterisk means &quot;the dereferenced a&quot;!
评论 #25486043 未加载
xeromalover 4 years ago
That was a fun little exercise