Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use types in <cstdint> with or without namespace

Tags:

c++

c++11

stl

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

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

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

so variant a):

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

resp:

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

or variant b):

#include <cstdint>
uint8_t n = 21;
like image 307
tyrondis Avatar asked Oct 29 '12 21:10

tyrondis


People also ask

What can I use instead of using namespace std in C?

The alternative is to write std:: everywhere.

What is Stdint h in C++?

h is a header file in the C standard library introduced in the C99 standard library section 7.18 to allow programmers to write more portable code by providing a set of typedefs that specify exact-width integer types, together with the defined minimum and maximum allowable values for each type, using macros .

Is using namespace std a library?

Including a library header makes the library feature visible to your program code. Without that, your program has no idea that the library even exists. This is the part that is necessary. Writing using namespace std simply allows you to write cout rather than the full name which is std::cout .


2 Answers

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

Except as noted in Clauses 18 through 30 and Annex D, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in the C standard library (1.2) or the C Unicode TR, as appropriate, as if by inclusion. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations (7.3.3).

If you use the names from the <cname> headers without std, your program is relying on unspecified requirements.

This was different in C++03 and earlier where names were only supposed to appear in the std namespace. However, the reality was that many implementations were simply injecting the contents of the C standard library headers <name.h> into std and so this was accommodated for in C++11. The corresponding section (§17.4.1.2/4) from the C++03 standard says:

Except as noted in clauses 18 through 27, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in ISO/IEC 9899:1990 Programming Languages C (Clause 7), or ISO/IEC:1990 Programming Languages—C AMENDMENT 1: C Integrity, (Clause 7), as appropriate, as if by inclusion. In the C++ Standard Library, however, the declarations and definitions (except for names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std.

Further to this, qualifying names with std:: helps to avoid collisions - you know exactly what you're getting if you fully qualify it. If you're really going to do using namespace std or using std::something, at least do it in as minimal a scope as you can.

like image 119
Joseph Mansfield Avatar answered Oct 05 '22 02:10

Joseph Mansfield


In C++11, for the C headers that are explicitly named in the C++ standard, the following holds:

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

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

like image 36
Sjoerd Avatar answered Oct 05 '22 02:10

Sjoerd