Quote:Original post by OluseyiQuote:Original post by rozz666
This template can be useful anywhere you use sizeof(v) / sizeof(v[0])...
Which is only in places where you already know the size of the array, because you defined it in the same scope. It's quicker to just store the dimension you statically requested.
There are situations where this is a useful technique. The most common place I find it useful is where I have a static constant array whose size is implicit from the number of initializers. In that case it's handy to be able to get the size of the array using this technique without having to duplicate information by creating a size variable that must be updated if you add elements to the array. e.g.
namespace {const char* someConstantStrings[] = { "apple", "orange", "pear" };}void doSomethingWithStrings(){ for (int i = 0; i < lengthof(someConstantStrings); ++i) { doSomethingWithString(someConstantStrings); }}
[edit] Which now I look at it is pretty much exactly the use the OP was putting it to in his error example above.