Z
Zygmunt Krynicki
Yes. And it will produce a compile error when mis-used.
As stated in my comment, this is much safer than the classic sizeof trick.
I agree.
YET: the original problem was diffetent, to quote you if I may:
The size of the array needs to be passed as a second parameter.
No other way in C (C++ allows what you need, through templates...).
End of quote. So what we had problems with is to know the size of an array
inside a function without passing extra parameters. See below.
There is no such thing as a one true way to code in C++.
True, what I wanted to show was there are some good guidelines avalaible
that I agree with.
Let me get back to your original comment: you said that the usage
of templates would be overkill. Why ?
If they provide a safer alternative to C macro tricks,
why should one avoid them ?
As you didn't specify, at the time of responding to OP's question, that
you had this particular technique on mind. I assumed you were talking
about something like:
template <class T, int N>
void fill(T &a[N], const T &value)
{
for (int i=0; i<N; ++i) a = value;
}
which I still regard as an overkill due to uneccessary code bloating.
When I do some embedded programming, I feel my code can truly
benefit from some C++ techniques, including those that rely on
templates, even when standard library containers are not only
overkill, but totally of the question (i.e. w/ only 4Kb of RAM).
I'm not experienced in embedded programming but I'd rather stick to C in
your situation.
Regards
zygmunt Krynicki