I have a question about the following simple comparison:
#define BUF_SIZE //maybe large
static char buf[BUF_SIZE];
static char *limit; // some pointer to an element of buf array
void foo(){
if(limit - buf <= sizeof buf){ //<---- This comparison
//...
}
//...
}
Here we are comparing ptrdiff_t (on the left) which is signed and size_t (on the right) which is unsigned. The Standard provides the following explanation
6.5.8/3:
If both of the operands have arithmetic type, the usual arithmetic conversions are performed.
6.3.1.8/1 gives us 3 possibilities:
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
We don't know the conversion rank of ptrdiff_t and size_t. Moreover there is no in general a corresponding unsigned type for ptrdiff_t (unlike, say intptr_t and uintptr_t).
QUESTION: Suppose that the conversion rank of ptrdiff_t is strictly greater than of size_t and ptrdiff_t cannot represent all the values of size_t. What will happen when performing comparison between ptrdiff_t and size_t providing that there is no corresponding unsigned integer type for ptrdiff_t. Is such an implementation even allowed?
ptrdiff_t. We don't know what its name is, but it must exist. - riciunsigned. But according to 6.7.2/2unsigned ptrdiff_tis not a valid type specifier. - St.Antarioptrdiff_tis a type alias, the corresponding unsigned type is the corresponding unsigned type of the typeptrdiff_tis an alias of. Type aliases are not different types; they are different names for the same type. That's why I said we don't know the name of the corresponding unsigned type. The fact that we don't know how to declare it does not mean that it doesn't exist. - riciptrdiff_tmight be an alias of an extended integer type, whose name is in the part of the namespace reserved for the implementation. But it still will have a corresponding unsigned type. - ricisize_tandptrdiff_thave been discussed extensively over a span of more than two decades. Here, on mailing lists, on blogs, and most importantly in committee submissions. I don't think there's a high likelihood that another discussion will reveal anything new. If you are interested in implementations with extended integer types, you can certainly find them. I don't know if there is yet a consensus about the base names of such types; my sense is that type aliases are common. - rici