Your question is not without merit. In fact, standard library already has a very similar feature implemented as a macro: it is the standard offsetof
macro. Note also that a large portion of sizeof
's usability comes form its compile-time nature (also true for offsetof
), which is why a run-time implementation would be significantly inferior. (And, BTW, your assertive statement claiming that all other operators are "always executed at runtime" is completely untrue.)
One reason sizeof
could not be implemented as a macro even in the original C language was the fact that it was supposed to accept both types and expressions as arguments. It would be impossible to cover both kinds of arguments with a single macro. We'd need at least two macros: one for types and one for expressions. But even if we agree to separate these two forms of sizeof
into two different macros, it still would be rather difficult (or even impossible) to implement a macro that would handle expression arguments properly.
Meanwhile, if we restrict ourselves to the original C (i.e. exclude from consideration C99 VLAs), then we could probably implement sizeof
for type arguments as a macro
#define sizeof(T) ((size_t) ((T *) 0 + 1))
This is pretty much the same technique that is used to implement the standard offsetof
as a macro.
The above macro implementation is still not perfect, since it will not work properly for, say, int[6]
argument, but you get the idea. This, BTW, might be another point in favor of a built-in operator implementation instead of a macro.
But how to do the same thing for expression arguments - I don't know. Do you?