# C++ array question

**URL:** <https://boards.straightdope.com/t/c-array-question/133735>\
**Category:** Factual Questions\
**Created:** [October 24, 2002, 8:01pm UTC](https://boards.straightdope.com/t/c-array-question/133735 "2002-10-24T20:01:00Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [October 25, 2002, 4:36pm UTC](https://boards.straightdope.com/t/c-array-question/133735/21 "2002-10-25T16:36:41Z")

</div>

Would you believe that the capacity to do this is built into C++ with templates?

I’m looking at a paper by Todd Veldhuizen entitled “C++ Templates As Partial Evaluation”, in which he describes various weird things that you can do with templates in C++. One of them is performing compile-time calculations, and he gives the example of creating an array with a non-constant size using a strange sequence of template functions. This may be what the OP’s professor had in mind. The paper is available online, but I lost the link.

There are a few other tricks shown in there. It’s very interesting reading, especially if you thought that templates could only be used to write generic code.

---

<div class="post-metadata">

**Author:** ![William\_Ashbless](https://avatars.discourse-cdn.com/v4/letter/w/a87d85/32.png) [@William\_Ashbless](https://boards.straightdope.com/u/William_Ashbless)\
**Post date:** [October 25, 2002, 4:54pm UTC](https://boards.straightdope.com/t/c-array-question/133735/22 "2002-10-25T16:54:17Z")

</div>

I really have to disagree here.

I’d really like to see how anybody could invent a template (a compile-time generator of code) that could specify a _run-time specified_ array size _without_ dynamic allocation. Either the template you’re talking about is only allowing compile-time selection of array size (which doesn’t solve the problem), or its using dynamic allocation.

By definition a compiler can only work out compile-time issues from compile-time available information, which is all that templates have available to them. Unless I’ve misinterpreted the OP, the idea is to have run-time selectable array size.

Unless there’s some truly impressive uses for templates I don’t yet know about, which is perfectly possible! 😃

---

<div class="post-metadata">

**Author:** ![William\_Ashbless](https://avatars.discourse-cdn.com/v4/letter/w/a87d85/32.png) [@William\_Ashbless](https://boards.straightdope.com/u/William_Ashbless)\
**Post date:** [October 25, 2002, 5:31pm UTC](https://boards.straightdope.com/t/c-array-question/133735/23 "2002-10-25T17:31:57Z")

</div>

Okay, I’ve studied the paper (very cool paper by the way!), but I still don’t see how you could invent an array with a run-time decided array size using templates, but not using dynamic allocation.

I _will_ say that my solution above definitely would benefit from being templatized to turn the BLOCKSIZE, callback function type, element type, and parameter block type into compile time decisions and remove DynamicArray’s dependancy on specific types and constant sizes. I’ve pondered doing this just to prove it can be done. However, nothing can be done about the need to determine _at run-time_ the amount of space needed.

Please, I’d love to be proven wrong! 😃 It would mean I’d learn something even more twisted and demented about the language.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [October 25, 2002, 6:54pm UTC](https://boards.straightdope.com/t/c-array-question/133735/24 "2002-10-25T18:54:43Z")

</div>

> [@](#):
>
> \*Originally posted by William\_Ashbless \*  
> **Okay, I’ve studied the paper (very cool paper by the way!), but I still don’t see how you could invent an array with a run-time decided array size using templates, but not using dynamic allocation.**

Use the code in section 2.2 of that paper (which doesn’t compile under MSVC++ 6.0, grumble, grumble), but read in the values of Length and Dims. That should work, unless there’s some subtlety that I’m missing.

---

<div class="post-metadata">

**Author:** ![William\_Ashbless](https://avatars.discourse-cdn.com/v4/letter/w/a87d85/32.png) [@William\_Ashbless](https://boards.straightdope.com/u/William_Ashbless)\
**Post date:** [October 25, 2002, 7:10pm UTC](https://boards.straightdope.com/t/c-array-question/133735/25 "2002-10-25T19:10:20Z")

</div>

Yes… the length and dims needs to be _constant at compile time_. No compiler should accept run-time valued parameters for the template parameters in that context.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [October 25, 2002, 7:11pm UTC](https://boards.straightdope.com/t/c-array-question/133735/26 "2002-10-25T19:11:43Z")

</div>

Ah. Well, I stand corrected then.

---

<div class="post-metadata">

**Author:** ![sinemora](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@sinemora](https://boards.straightdope.com/u/sinemora)\
**Post date:** [October 25, 2002, 7:37pm UTC](https://boards.straightdope.com/t/c-array-question/133735/27 "2002-10-25T19:37:08Z")

</div>

> [@](#):
>
> So, anyway, can someone try out what I did on gcc or Borland Builder or something non-Microsoft? Perhaps CodeWarrior or something on a Mac?

It works on a Sparc with the Sun compiler. What a wicked program you have got there… 🙂

---

<div class="post-metadata">

**Author:** ![William\_Ashbless](https://avatars.discourse-cdn.com/v4/letter/w/a87d85/32.png) [@William\_Ashbless](https://boards.straightdope.com/u/William_Ashbless)\
**Post date:** [October 25, 2002, 8:04pm UTC](https://boards.straightdope.com/t/c-array-question/133735/28 "2002-10-25T20:04:20Z")

</div>

Woohoo! 😃 😃 😃

I’m… somewhat astonished… and horrified at myself…

[Previous page](https://boards.straightdope.com/t/c-array-question/133735.md?page=1)
