Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: What library to include when only wanting to use size_t? [duplicate]

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?

like image 854
Qqwy Avatar asked Nov 27 '25 00:11

Qqwy


2 Answers

You can use #include <cstddef>

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

like image 63
Max Avatar answered Nov 29 '25 13:11

Max


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.

like image 23
Cheers and hth. - Alf Avatar answered Nov 29 '25 13:11

Cheers and hth. - Alf