2

I am learning C++. One of the data members of my classes is supposed to be of type size_t. Now size_t is not a built-in primitive type but rather a defined type (as an unsigned value that usually is at least as large as an int) that is heavily used all around C++.

Many of the C++ libraries, such as iostream or string will automatically add size_t to your current scope.

However, my class does not need to include any fancy libraries. As I want to keep it as lean as possible, I only want to include the file responsible for creating size_t.

Which library is this?

Qqwy
  • 5,214
  • 5
  • 42
  • 83
  • 2
    [Pick one](http://en.cppreference.com/w/cpp/types/size_t). For your stated desire, i'd probably go with `` – WhozCraig Sep 27 '16 at 06:03
  • 2
    I sometimes do this, when nobody is watching: `typedef decltype(sizeof(1)) size_t;` It won't cause an error because `size_t` is supposed to be the type given by the `sizeof` operator, and type re-definitions only fail when the base type is different than a previous definition. It may be more palatable if enclosed in your own namespace, though. – Christopher Oicles Sep 27 '16 at 06:08

2 Answers2

5

You can use #include <cstddef>

Also see here for details http://www.cplusplus.com/reference/cstddef/

Max
  • 852
  • 1
  • 9
  • 19
4

size_t is defined by <stddef.h>, in both C++ and C.

In C++ you can alternatively include <cstddef>, and then write std::size_t. Plain size_t may compile or not, but is not guaranteed available with this header. However, I don't recommend this, since it has no advantage, and has the problem that if you forget the std::, or it's not there in other code that gets included, the code may fail to compile with some other compiler.

Due to the problems with implicit conversion to unsigned common type in expressions, consider using signed ptrdiff_t instead of size_t, with the exception of template parameters. The exception is because at least one much used compiler has problems matching a non-size_t template parameter with e.g. a raw array size.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Downvoter, please explain the asinine downvote. Is it... because of the mention of a certain compiler's shortcomings? Yes? – Cheers and hth. - Alf Sep 27 '16 at 08:38
  • I'm not the downvoter, but probably it is due to the fact that you suggest not to use `cstddef` and instead prefer `stddef.h` in C++. Mixing C and C++ code is not a matter of preferring a `std::`-less type actually. – skypjack Sep 27 '16 at 10:20
  • @skypjack: Well maybe but that would be inane, as stated. Using `cstddef` has no advantages and it does have problems. It's there for historical reasons, and the support for original rationale was removed in C+11. – Cheers and hth. - Alf Sep 27 '16 at 17:08
  • @skypjack: Regarding your strawman argument "Mixing C and C++ code is not a matter of preferring a std::-less type actually", one would have to be extraordinarily challenged in order to believe that in the first place. Where the *!"#& did you get the inspiration for that? – Cheers and hth. - Alf Sep 27 '16 at 17:11
  • I even don't know what _strawman_ means!! That's one of the problems of non-native speakers. On the other side, _*!"#&_ is quite common. :-) – skypjack Sep 27 '16 at 17:27