Yes, absolutely, it is worth catching this at compile-time.
But what you describe above wouldn't catch that at compile-time, because
the compiler would use this operator to get an implicit conversion to
size_t followed by implicit conversion from size_t to uint32_t.
You could use MFBT's new MOZ_EXPLICIT_CONVERSION to annotate this
conversion operator with 'explicit' on supporting C++11 compilers. But
instead, I would not bother trying to make this a conversion operator, I
would just make this a plain old getter method.
I would also not call that Size, but IndexOfResult. The problem is that
what IndexOf() returns is not necessarily a "size" or "index", it may also
be this special thing called "NoIndex". So I don't think that its return
type should "feel like" it _is_ just an index. Instead, its return type
should be something that is _either_ an index or NoIndex.
Something like this:
class IndexOfResult {
const index_type mIndex;
static const index_type NoIndex = index_type(-1);
public:
IndexOfResult(index_type index) : mIndex(index) {}
IndexOfResult(const IndexOfResult& other) : mIndex(other.mIndex) {}
bool Found() const {
return mIndex != NoIndex;
}
index_type Value() const {
MOZ_ASSERT(Found());
return mIndex;
}
};
Benoit