Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preferred include header for std::size_t

Tags:

c++

std::size_t is defined in any of:
<cstddef> <cstdio> <cstdlib> <cstring> <ctime> <cwchar>

Which is considered 'kosher' for getting just std::size_t?

like image 911
sp2danny Avatar asked Dec 21 '14 19:12

sp2danny


People also ask

What header contains Size_t?

size_t is a base unsigned integer memsize-type defined in the standard library of C/C++ languages. This type is described in the header file stddef.

Should I use std :: Size_t or Size_t?

The only time that using std::size_t is "better" than ::size_t is if you have not included <stddef. h> (perhaps you have #include <cstddef> instead).

Should I use Size_t in C++?

Use size_t for variables that model size or index in an array. size_t conveys semantics: you immediately know it represents a size in bytes or an index, rather than just another integer. Also, using size_t to represent a size in bytes helps making the code portable. Save this answer.

Should I use Size_t instead of int?

If we consider the standard, both are integers of size 16 bits. On a typical 64-bit system, the size_t will be 64-bit, but unsigned int will be 32 bit. So we cannot use them interchangeably. One standard recommendation is that the size_t be at most as big as an unsigned long.


1 Answers

Since this is part of the C library I think the C standard specified header is the right one: stddef.h, i.e. cstddef.

From C11:

7.19 Common definitions

The header defines the following macros and declares the following types. Some are also defined in other headers, as noted in their respective subclauses.

[...] size_t which is the unsigned integer type of the result of the sizeof operator;

This is after all a C++ question so I think a quote from the C++ standard is more appropriate:

18.2 Types

Table 30 describes the header <cstddef>.

Types: ptrdiff_t size_t max_align_t nullptr_t

like image 181
cnicutar Avatar answered Oct 05 '22 16:10

cnicutar