Hi,
Designing my own array class, I ended up using the ptrdiff_t type for array indexing. The fact that this type is signed make it safer for loops such as
for (ptrdiff_t k = 0; k < v.size() - 1; ++k) { v[k] = v[k + 1]; } for (ptrdiff_t k = v.size() - 1; k >= 0; --k) { v[k] = 0.0; }
Because ptrdiff_f sounds weird, I have a typedef ptrdiff_t index_t. The more I think about it, the more I find using size_t for arrays indexing a huge flaw of the C++ language. Reading Stroustrup book on C++ where he says that using unsigned types for integers that should be always nonnegative is often a bad design. On this page from Intel, https://software.intel.com/en-us/articles/about-size-t-and-ptrdiff-t , it is said that ptrdiff_t is a natural choice for array indices.
So my question is:
- Is there any compiler optimisation enabled by using size_t instead of ptrdiff_t for array indices ?
- Why does the STL use size_t instead of ptrdiff_t for array indices ?