Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which C++ compilers automatically define size_t without requiring a header include?

While using the C++ compiler of Visual Studio 2013, I noticed that my code relying on size_t correctly compiled even without including any headers that define it (i.e. #include <stddef.h> or #include <string.h>).

I tested this because I kind of like the idea of not including a whole header just for something trivial like that; I feel like it bloats my code. I concluded that the C++ compiler of Visual Studio 2013 automatically defines the size_t, even without any header inclusions in the code.

While enjoying this, I started worrying about portability. Coding convenience, and feeling like my code is elegant, are things more important to me than absolute portability; but I would still like some portability. For example, I don't mind using #pragma once since most compilers support it and header guards are a hassle (especially so in Visual Studio), but I would never resort to exporting templates just because one comiler supports it.

So, my question is, is the automatic definition of size_t a widespread feature offered by many compilers, or is it something specific to Microsoft's compiler?

like image 924
RPFeltz Avatar asked Dec 09 '22 04:12

RPFeltz


2 Answers

This behaviour certainly isn't widespread; both GCC and Clang require the inclusion of <cstddef> for std::size_t, or the deprecated <stddef.h> for ::size_t, as the standard specifies.

like image 113
Mike Seymour Avatar answered May 12 '23 12:05

Mike Seymour


Neither gcc nor clang contain a similar violation of the C++ standard.

Between the 3, MSVC gcc and clang are the vast majority of compilers out there.

The same is true of icc 13.0.1.

I did not try ECG, feel free yourself.

like image 32
Yakk - Adam Nevraumont Avatar answered May 12 '23 13:05

Yakk - Adam Nevraumont