2

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:

  • 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?
  • 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?
  • 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?
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
  • Here's [a link](https://stackoverflow.com/questions/1119370/where-do-i-find-the-definition-of-size-t) you'll find your answer –  May 14 '18 at 04:45
  • @shantanusingh I don't see the answer to my question at the link you provided. Can you point me to the answer that discusses whether there are pros and cons or surprises I need to consider while including a specific header file to get `size_t`? – Lone Learner May 14 '18 at 04:51
  • In **C**, a header introduces all the functions and definitions as specified for that header. It may introduce more, but those reside in the namespace reserved for the implementation (leading `__` or leading `_` with uppercase letter). In C++, headers are allowed to include *other* headers, but then again, in C++ you get the namespace `std::` so that should not be a problem. You usually go with whatever header you need to include anyway for whatever you are doing in your program. – DevSolar May 14 '18 at 04:55
  • `` is slightly smaller than the others, so if you don't need anything else... – Bo Persson May 14 '18 at 10:10

2 Answers2

3

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @user202729, I don't think that is necessary. The question is not about where you can find the definition of `size_t`. The post already contains that. – R Sahu May 14 '18 at 04:54
  • I see you made an edit in your answer to change your preference from `` to ``. Did your preference change now while writing the answer or was the earlier answer a typo? What led to this change? – Lone Learner May 14 '18 at 05:09
  • @LoneLearner, I don't know whether to call it a typo. It's more like the mind and fingers were not in sync. – R Sahu May 14 '18 at 05:13
2

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.

101010
  • 41,839
  • 11
  • 94
  • 168
  • 1
    A useful note IMHO is that your own definition appears to be *the* definition, at least according to [cppreference](http://en.cppreference.com/w/cpp/types/size_t) so there's are pretty good chance it's the standard definition. – luk32 May 14 '18 at 14:19
  • @luk32 Note added thnx – 101010 May 14 '18 at 16:02