c++ - Is the size of std::array defined by standard -
c++ - Is the size of std::array defined by standard -
in c++11 std::array
defined have contiguous storage , performance no worse array, can't decide if various requirements of standard imply std::array has same size , memory layout normal array. can count on sizeof(std::array<int,n>) == sizeof(int)*n
or implementation specific?
in particular, guaranteed work way expect to:
std::vector< std::array<int, n> > x(m); typedef (*arraypointer)[n]; arraypointer y = (arraypointer) &x[0][0]; // utilize y normal multidimensional array
it works in 2 compilers tried (gnu & intel). furthermore, 3rd party documentation find (like this), states std::array memory efficient plain array, combined contiguous requirement imply must have identical memory layout. can't find requirement in standard.
it's nearly required. specifically, §23.3.2.1/2 says:
an array aggregate (8.5.1) can initialized syntax
array<t, n> = { initializer-list };
where initializer-list
comma-separated list of n elements types convertible t.
since it's aggregate, can't utilize sort of constructor convert info in initializer-list right format. leaves 1 possibility: thing can store values themselves.
i suppose would possible std::array
store sort of auxiliary info next specified data, such memory set predefined value, if write past end of array, you'd alter data. compiler/run-time check values @ shut-down, , if you'd changed values, study code's undefined behavior.
it's possible compiler could padding/alignment differently std::array
built-in array. 1 obvious illustration desirable back upwards super-alignment requirements, such info utilize intel's sse instructions. built-in array can't back upwards super-alignment, think specification of std::array
might barely loose plenty allow it.
bottom line: without getting questions of how many possibilities might exist, it's pretty clear std::array
doesn't have follow rule you're asking about.
c++ c++11 stl language-lawyer
Comments
Post a Comment