Monday, 26 December 2011

size_t

It bothers me that size_t, the result of sizeof, is in a header. Why should I have to pull in <stddef.h> or <cstddef> just to get the name of something which the compiler knows intrinsically?

Fortunately, C++11 provides a solution:

decltype(sizeof 0)

The expression in the sizeof is immaterial; I chose 0 because it was short and it already has special meaning in the language, so it doesn't look too out of place.

No comments:

Post a Comment