On 2014-05-11, 10:15 AM, Benoit Jacob wrote:
Hi,

Since Bug 1004098 landed, the type of nsTArray lengths and indices is now
size_t.

Code using nsTArrays is encouraged to use size_t for indexing them; in most
cases, this does not really matter; however there is one case where this
does matter, which is when user code stores the result of
nsTArray::IndexOf().

Indeed, nsTArray::NoIndex used to be uint32_t(-1), which has the value 2^32
- 1.  Now, nsTArray::NoIndex is size_t(-1) which, on x86-64, has the value
2^64 - 1.

This means that code like this is no longer correct:

   uint32_t index = array.IndexOf(thing);

Such code should be changed do:

   size_t index = array.IndexOf(thing);

Or, better still (slightly pedantic but would have been correct all along):

   ArrayType::index_type index = array.IndexOf(thing);
>
Where ArrayType is the type of that 'array' variable (one could use
decltype(array) too).

Do you think it's worth trying to make the bad code above not compile, by returning an object from IndexOf which only provides an implicit conversion to size_t and not uint32_t? Like:

template <class T>
class nsTArray {
  // ...
  struct Size {
    Size(size_t);
    operator size_t() const;
  };
  Size IndexOf(...);
};
_______________________________________________
dev-platform mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to