I have a small program which compares
(1) sizeof,
(2) numeric_limits::digits,
(3) and the results of a loop
in an effort to make sure they all report the same thing regarding the size of the “int types” on any C++ implementation. However because I don’t know about the internals of sizeof, I have to wonder if it is just reporting numeric_limits::digits. Thanks
Most likely
sizeof()on most compilers causes the compiler to look the given type (or object’s type) up in its internal type table and insert a literal for that type’s defined size into the code it generates. This would happen at compile time, not runtime.To answer the question in the comments, there isn’t any language-defined access to the compiler’s internals in C++ (outside of things like
sizeof()itself, of course). The only similar language I know of that lets you do stuff like that is Ada, which provides ASIS for writing compiler-independent code analysis tools.