RPFeltz
RPFeltz

Reputation: 1075

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?

Upvotes: 6

Views: 827

Answers (2)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275385

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.

Upvotes: 4

Mike Seymour
Mike Seymour

Reputation: 254461

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.

Upvotes: 10

Related Questions