Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ std:: types instead of corresponding c types

Tags:

c++

For instance, consider the <cstdint> header. C++ standard says:

The header defines all types and macros the same as the C standard library header <stdint.h>.

So is there a need to use say std::int8_t instead of the short C form int8_t somewhere?

like image 885
αλεχολυτ Avatar asked Dec 12 '18 13:12

αλεχολυτ


People also ask

What is int64 C++?

The __int8 data type is synonymous with type char , __int16 is synonymous with type short , and __int32 is synonymous with type int . The __int64 type is synonymous with type long long .

How many C types are there?

Main types. The C language provides the four basic arithmetic type specifiers char, int, float and double, and the modifiers signed, unsigned, short, and long.

What is int8_t C++?

int8_tint16_tint32_tint64_t. (optional) signed integer type with width of exactly 8, 16, 32 and 64 bits respectively. with no padding bits and using 2's complement for negative values.


1 Answers

So is there a need to use say std::int8_t instead of the short C form int8_t somewhere?

Yes. Per [headers]/4 it is unspecified if the C names are defined in the global namespace or not. Because of this you need std::int8_t to use int8_t. It doesn't guarantee that std::int8_t exists, just that if it does exist, using std::int8_t is guaranteed to work.

You can just add using std::int8_t; so you don't have to type std::int8_t all over the place.


The relevant part of [headers]/4 is

In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope of the namespace std

emphasis mine

This means we know they are declared in std::. The part

It is unspecified whether these names (including any overloads added in [language.support] through [thread] and [depr]) are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations ([namespace.udecl]).

then allows the names to be in the global namespace, but does required it. This lets implementations do things like

<cstdint>:

namespace std
{
    #include <stdint.h>
}

or

<cstdint>:

#include <stdint.h>
namespace std
{
    using ::int8_t;
    using ::int16_t;
    //...
}

Bot accomplish the same thing (putting the names in std::) and are legal implementations but only the second one puts the names in the global scope.

like image 64
NathanOliver Avatar answered Sep 28 '22 04:09

NathanOliver