Use types in <cstdint> with or without namespace

In C ++ 11, I can choose whether I want to use types defined with or without the std :: namespace

At least my compiler (g ++ 4.7) accepts both.

My question is: what is the recommended way to use typedefs from cstdint. With or without a namespace? What are the advantages or disadvantages? Or is it just a matter of style?

so option a):

#include <cstdint> std::uint8_t n = 21; 

respectively:

 #include <cstdint> using std::uint8_t; uint8_t n = 21; 

or option b):

 #include <cstdint> uint8_t n = 21; 
+8
source share
5 answers

Prefer names declared in the std . The reason is specified in Β§17.6.1.3 / 4 (ISO / IEC 14882: 2011 (E), C ++ 11):

Except as specified in paragraphs 18-30 and Appendix D, the contents of each cname header shall be the same as the contents of the corresponding .h header, as specified in the C standard library (1.2) or in Unicode TR code, as the case may be, as if by inclusion. However, in the C ++ standard library, declarations (with the exception of names that are defined as macros in C) are in the namespace (3.3.6) of the std . It is not known whether these names are declared for the first time in the global namespace and then entered into the std namespace using explicit use-declarations (7.3.3).

If you use names from <c name > headers without std , your program relies on unspecified requirements.

This was different in C ++ 03 and earlier, where names should only display in the std . However, the reality was that many implementations simply inserted the contents of the headers of the standard C < name .h> library into std , and therefore this was done for C ++ 11. In the corresponding section (Β§17.4.1.2 / 4) from the C + standard + 03 it says:

Except as noted in Articles 18 to 27, the contents of each cname header must be the same as the corresponding .h header name as specified in the programming languages ​​C / C: or ISO / IEC: 1990 Programming Languages-C AMENDMENT 1 : C Integrity, (p. 7), depending on the situation, as if by inclusion. However, in the C ++ standard library, declarations and definitions (with the exception of names that are defined as macros in C) are in the namespace (3.3.5) of the std .

In addition, qualification names with std:: help to avoid collisions - you know exactly what you get if you fully qualify it. If you are really going to do using namespace std or using std::something , at least do it as minimal space as you can.

+6
source

In C ++ 11, for C headers that are explicitly specified in the C ++ standard, the following is true:

  • Implementation is required for versions <foo.h> to add them to the global namespace and is allowed to add them to std:: namespace.

  • An implementation is required for versions of <cfoo> to add them to the std:: and is allowed to add them to the global namespace.

+4
source

The reason for wrapping things in the std in the <cstdint> header is to avoid name conflicts that are pretty annoying when they happen. However, in this case, it is unlikely that the types will be found anywhere else. Therefore, I would use <stdint.h> , especially because this function was introduced in C before it was added in C++ , and therefore the <stdint.h> header is older than <cstdint> and therefore available in old compilers.

If you decide you want to use these names in the global namespace, you should also give preference to <stdint.h> before <cstdint> , and then use using namespace std , as the latter discards all other std things from other <cfoo> yhou headers as well included in the global namspace, which you probably don’t want, since many other standard names are much more prone to conflict than similar uint8_t ones.

+2
source

My personal style is always to fully qualify names so that it is clear where they came from. That is, I would use std::uint8_t . That is, I would include <cstdint> and use qualified names.

However, note that using std::uint8_t is only specified if you really want to use a type with exactly 8 bits. If the platform on which your code is running is not of this type, for example, since it uses 9-bit blocks as the main object, the program should not be compiled. If you want to use the smallest unsigned with 8 bits, you want to use uint_least8_t .

+1
source

Include <cstdint> and use std:: or include <stdint.h> to use unqualified type names. There are several platforms (for example, QNX SDP 6.6) on which <cstdint> does not declare these types in the global namespace.

0
source

All Articles