c++11 - Implementing a fixed run-time size array. Should move ctor and swap throw exceptions? -


the problem std::array has fixed compile-time size. want container can created dynamic size, size stays fixed throughout life of container (so std::vector won't work, because push_back increment size 1).

i wondering how implement this. tried writing class contains internal std::vector storage, , exposes members of std::vector don't change size. question regarding copy/move assignment operators, swap member function. usually, move assignment declared noexcept. however, before assignment, have check if lhs , rhs of same size. if not, must throw exception, because otherwise, assigning rhs lhs change size of lhs, don't want. same happens swap, in implementation noexcept same reason.

i know going against usual advice make swap , move assignment noexcept (item 14 of scott meyers' modern effective c++), wondering if design? or there better way implement fixed runtime size container?

example: suppose have defined fixed size container name fixedsizearray<t>.

auto arr1 = fixedsizearray<double>(4, 1.0) 

the last line of code defined fixedsizearray containing doubles of size 4. define another:

auto arr2 = fixedsizearray<double>(10, 1.0) 

should following line:

arr1 = std::move(arr2) 

throw? about:

arr1.swap(arr2) 

do declare move assignement , swap noexcept. don't throw on mismatched sizes...

since arrays fixed-size, ending assigning or swapping 2 arrays of different sizes can't possibly work, in circumstances. it's not exceptional condition, it's situation program doesn't know it's doing. that's case assertion.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -