Re: retrieve size of array
kanze wrote:
Andrei Polushin wrote:
When the "independence" is the issue, then we might look deeper and
further, and notice that many public interfaces avoid dependence on
std::vector, but prefer C-style arrays, because of their predictable
binary representation.
Most such public interfaces avoid dependence on C++ completely,
and use a public interface defined in C. In practice, if
std::vector changes its layout, there's a pretty good chance
that vtable layout will change as well, as will name mangling,
and who knows what all else. Either the compiler guarantees
binary compatibility (which includes the layout of all of the
standard classes), or it doesn't.
The reality is not black and white ("either guarantees or not"), but
we can talk about the measure of stability in each case. On my system
(Windows) the vtable layout remains stable for the past 15 years
(due to COM requirements), so it is more stable than the binary
representation of STL classes, which stability is not controlled
by some binary standard or community conventions. Similarly in C,
the stability of (sizeof int) was about 20 years - quite enough!
If that is not enough, we can use more stable types like int32_t.
And so on: when we want an interface to be more or less stable, we
should depend on more or less stable things. Currently, C++ doesn't
mandate the binary representation of many so standard types, class
layouts, struct alignments, so it is less (not more) usable in public
APIs than C. That's a pity, because C++ is neither an interpreted
language, nor a virtual-machine language (like Java) and binary
compatibility is a very important thing here.
--
Andrei Polushin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]