Reputation: 20628
According to http://en.cppreference.com/w/cpp/types/size_t , the type size_t
is defined in many header files: cstddef, cstdio, cstdlib, etc.
While writing my own code which header file should I include to use size_t
?
This might seem like a trivial question, but as a beginning of C++ I am concerned about the following:
size_t
would behave in the same way regardless of which header file I included?size_t
?Upvotes: 2
Views: 1064
Reputation: 42899
Is there a coding convention or popular convention or practice regarding this that leads to most people including a specific header file to get the definition of size_t?
No, there's not or at least none that popular that I know of. Personally, in cases where I only need std::size_t
, in order not to drag unnecessary code from the headers that define std::size_t
, I define my own size_t
as:
using size_t = decltype(sizeof(char));
Note: The above also complies with the standard definition of std::size_t
.
Upvotes: 2
Reputation: 206577
Can I include any header file and be sure that
size_t
would behave in the same way regardless of which header file I included?
Yes.
Are there any surprises I need to be aware of like including one header file would have surprising side-effects that including another header file would not have?
No.
Is there a coding convention or popular convention or practice regarding this that leads to most people including a specific header file to get the definition of
size_t
?
I personally prefer <cstddef>
but I am not aware of any conventions.
Upvotes: 3