Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 33,214 of 33,346    |
|    Francis Glassborow to All    |
|    Re: Is "T[N][]" (or "T[N][][M]") indirec    |
|    23 Sep 13 10:20:31    |
      From: francis.glassborow@btinternet.com              On 23/09/2013 02:24, Daniel Krügler wrote:       > Am 20.09.2013 15:06, schrieb Daryle Walker:       >> On Wednesday, September 18, 2013 2:20:09 PM UTC-4, Daniel Krügler wrote:       >> No, I mean things like I said in the Subject line:       >>       >> T[N][]       >> T[N][][][M]       >>       >> The compiler (rightfully) prevents these when I use these lexical forms.       >> But can I create them indirectly via typedefs?       >       > No, you cannot, because those are excluded from valid types, see my       > previous quote referring to 8.3.4 [dcl.array] p1. This was already clear       > in C++03, where we find a similar constraint in above mentioned paragraph:       >       > "When several “array of” specifications are adjacent, a multidimensional       > array is created; the constant expressions that specify the bounds of       > the arrays can be omitted only for the first member of the sequence."                     Daniel has given you the technical answers based on the Standard, but       perhaps it would help if I explain the reasons for those restrictions       and why, in a statically bound (i.e. uses a compiler+linker to produce a       stand alone executable) there is not a way to do it differently and       retain the fundamental efficiencies of arrays.              Raw arrays (which is what we are dealing with here) were designed in C       for maximum efficiency both of speed and space. No extra meta-data about       an array object is stored so that an array of any number of dimensions       uses only storage for the values. The speed aspect is also provided       because the running code does not have to look up any meta-data and       simply trusts the programmer to get it right. Array data is stored as a       single contiguous block.              In order to support dynamically sized arrays (using malloc in C or new       in C++) means that access to data is basically through calculating       addresses (whose values might be stored in pointers for dynamic arrays).       When we pass information about arrays as arguments to a functions       parameters we do so either explicitly or implicitly by passing a pointer.              The compiler needs to know how to set up the calculation of the next       element of an array from the address of the current one. To do that it       needs to know how the size of the current object so that it can compute       where the next object ill start.              In a multi-dimensioned array it can always work out the step from a       single element to the next because it knows the size of the element from       the elements type. However to calculate the start of the next 'row' it       needs to know how many elements make up a row. To calculate where the       next 'page' starts it needs to know how many rows make up a page, etc.              This is why we can sometimes omit the first dimension, the compiler can       work out the size of an element from the type of an element, but it       needs to know more to work out other dimensions.              It is worth noting that there is no mechanism for directly creating a       multi-dimensioned dynamic array. All dynamic arrays have a single       dimension and to work with multiple dimensions dynamically the       programmer has to do much more (working with arrays of pointers etc.)              All this is implicit in a statically typed language such as C++ and can       be quite strange to someone coming from a dynamically typed language       such as Python or Ruby.              By the way, despite some claims in the past, Java (also statically       typed) has pointers, they are just not directly available for       computation by the programmer.              Hope this helps (and others will refine and correct my errors etc.)       Francis                                                 --        [ See http://www.gotw.ca/resources/clcm.htm for info about ]        [ comp.lang.c++.moderated. First time posters: Do this! ]              --- SoupGate-Win32 v1.05        * Origin: you cannot sedate... all the things you hate (1:229/2)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca