CppCoreGuidelines
CppCoreGuidelines copied to clipboard
There is a need for a guideline regarding C names in the std namespace?
There is a need for a guideline regarding C names in the std namespace?
- No need to mention it, it doesn't matter if you use size_t (::size_t?)
- The guidelines should explicitly recommend std::size_t?
- The guidelines should explicitly say it doesn't matter?
if you #include <cstddef>, you can use std::size_t, if you #include <stddef.h>, you can use size_t
As for which headers to include for C library types (stddef.h vs cstddef, stdint.h vs cstdint, stdio.h vs cstdio etc), I suspect a consistent guideline would say "C++ headers every time, the others are deprecated and may not work" (the latter applies only to math.h/cmath to my knowledge, where cmath adds a ton of overloads and templates that C didn't have but a C++ program may rely on)
if you #include
, you can use std::size_t, if you #include <stddef.h>, you can use size_t
Is this what the standard says? (I had a quick read now and it wasn't 100% clear to me) I always understood that cstddef defines the types in the std namespace in addition to also in the global namespace. That's what all the implementations I have tried do.
If, by the standard, you can't rely on ::size_t being there after #include <cstddef> that's a stronger argument for "always use std::size_t, never ::size_t".
Is this what the standard says?
Yes.
I always understood that cstddef defines the types in the std namespace in addition to also in the global namespace.
No. <cstddef> is guaranteed to define the names in namespace std and allowed to define them in the global namespace, but not guaranteed to.
<stddef.h> is guaranteed to define the names in the global namespace and allowed to define them in namespace std, but not guaranteed to.
So the only guaranteed behaviour is that <cstddef> defines names in std and <stddef.h> defines names in the global namespace. That's all you can rely on portably, and so "if you #include <cstddef>, you can use std::size_t, if you #include <stddef.h>, you can use size_t" is correct.