There is nothing magic about const and templates. const works the same way for templates as for non-templates.
For instance, this works:
1 2 3 4 5 6 7 8 9
|
class Foo {
public:
void Frobnicate() const;
};
int main() {
Foo f;
f.Frobnicate();
}
| |
Frobnicate is a const method, which means it really wants to be called on a const Foo instance, yet
f is clearly not const. The compiler is allowed to implicitly add const qualifiers. So is the case for
ArraySize above, except that it is adding const to all elements of the array.
@coder777:
I only stepped into this thread because you gave an emotional argument for why you'd use numof()
instead of Disch's solution, whereas Disch gave a technical reason why to use his over yours. While
numof() may work for you, if I were in a code review in which I reviewed numof(), I would mark it
as at least a maintainability defect (because it is nothing more than an alias for the sizeof trick and
is susceptible to the same misuse which I documented above... ie, it is zero value-add) if not an
outright major error because of the potential for misue, particularly when a bulletproof solution exists.
I only continued to reply because I felt you continued to give emotional arguments against templates.
Since the purpose here is to provide help to posters asking questions, the best answers are technical
answers.
But I'm not trying to quash opinions; that's what the lounge is for. And besides, sometimes there
aren't answers that are unilaterally "best", such as the debate over source code formatting, tabs or
no tabs, etc. In those cases, the only answers are opinions.