C++: Vector bounds

后端 未结 4 1511
北海茫月
北海茫月 2020-12-11 01:40

I am coming from Java and learning C++ in the moment. I am using Stroustrup\'s Progamming Principles and Practice of Using C++. I am working with vectors now. On page 117 he

4条回答
  •  眼角桃花
    2020-12-11 01:55

    This is a valuable comment by @Evgeny Sergeev that I promote to the answer:

    For GCC, you can -D_GLIBCXX_DEBUG to replace standard containers with safe implementations. More recently, this now also seems to work with std::array. More info here: gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode.html

    I would add, it is also possible to bundle individual "safe" versions of vector and other utility classes by using gnu_debug:: namespace prefix rather than std::.

    In other words, do not re-invent the wheel, array checks are available at least with GCC.

提交回复
热议问题