"what if std::_Array_iterator<_Ty,_Size>::difference_type and ptrdiff_t are different types?" Do not use such a compiler. In addition, it is likely that it cannot formally differ. For instance. this is the case for vector using the standard default allocator, since the one where it retrieves its typedefs, but since the formal guarantee does not matter (he is, it really is not) I will not look at this in C ++ 0x draft .
So use ptrdiff_t .
But it might be a good idea to add a few typedef, for example
typedef ptrdiff_t Size; typedef ptrdiff_t Index;
and then in your specific case you use Index .
These typedefs, of course, are accompanied by customizable stand-alone functions countOf , startOf and endOf , which allow handling raw arrays and containers of the standard library in exactly the same way.
If you see the name Index , then itβs more clear that this is an index that, naturally, cannot get out of the set of Index or Size types almost regardless of what you do. For example, add something else to it, Index . So basically there won't be βanother dozen warnings.β
But in some rare cases, you need to switch from Index to just int , say. In and on these rare occasions, just do static_cast to shut down the compiler and make your intention clear. Or even the usual static_cast -like narrowTo operation, for expressiveness ...
Cheers and hth.,
source share