Should I include stddef.h or cstddef for size_t

fredoverflow picture fredoverflow · Feb 22, 2011 · Viewed 14.8k times · Source

When I want to use size_t in C++, should I include <stddef.h> or <cstddef>? I have heard several people saying that <cstddef> was a bad idea, and it should be deprecated. Why is that?

Answer

Armen Tsirunyan picture Armen Tsirunyan · Feb 22, 2011

stddef.h is the C header. The name size_t is in global namespace in it. <cstddef>, on the other hand, is a C++ header which wraps the C names into std namespace, which is naturally the C++ approach, so if you include <cstddef> and the compiler is compliant you'll have to use std::size_t . Clearly, in C++, C++ approach is more appropriate. HTH

Edit: Technically, the C header too may contain the names in the std namespace. But the C-headers (those that end with .h) introduce the names also to the global namespace (thus polluting it).