Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 'int' used as just int and why not as std::int in C++?

Tags:

c++

std

standards

Unlike everything is standardized in current C++, are there any specific reasons to leave int, char, ...., main() and others from it. (Not talking of +,-,%,.. because they aren't language-specific)

Why is it not like:

std::int std::main(std::int argc, std::char *argv[])
{
    //Sample C++ code (incorrect with current standards though)
    std::return 0;
}

Isn't standardization incomplete with them out of std scope?

What I believe is, they are basic components which occurs everywhere when writing a program, whether simple or complex. They are not included in the standardization, just to follow the DRY principle.

like image 302
deadLock Avatar asked Mar 09 '20 10:03

deadLock


People also ask

Should you use int32_t or int?

int/short/long etc might use a type that is faster for that particular target, potentially causing your application to behave differently. Using int32_t etc will make the behavior consistent, but possibly sacrifising performance in doing so.

What does int do in CPP?

C++ int. The int keyword is used to indicate integers. Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 2147483647.

Is there still a reason to use `int` in C++ code?

Is there still a reason to use `int` in C++ code? - Stack Overflow Bookmark this question. Show activity on this post. Closed 3 years ago. Many style guides such as the Google one recommend using int as a default integer when indexing arrays for instance.

Why do we use int in a loop instead of int?

It's a performance thing. A CPU works more efficient when the data with equals to the native CPU register width. This applies indirect to .NET code as well. In most cases using int in a loop is more efficient than using short. My simple tests showed a performance gain of ~10% when using int.

Why can't I use int in an API?

Many APIs use int, including parts of the standard library. This has historically caused problems, for example during the transition to 64-bit file sizes.

Why do we use int as indices in C++?

By using int as indices you one could signal wrong values/out of range with negative values, something that comes handy and can lead to a clearer code. "find index of an element in array" could return -1 if element is not present. For detecting this "error" you don't have to know the size of the array.


3 Answers

Keywords such as int and return and the main() function are all included in the C++ standard. std does not mean that only those things are standardized. Instead, it refers to the things that are in the standard library (which, like keywords, is a part of the standard). Include files such as #include <vector> are needed to use the standard library, but keywords can be used without any #includes.

like image 122
VLL Avatar answered Nov 15 '22 22:11

VLL


std:: is the namespace name of the Standard Library. But C++ has built-in types, and those are more fundamental. In fact, significant parts of the Standard Library are built using types like int. You can see the chicken-and-egg problem if the Standard Library would depend on itself.

like image 44
MSalters Avatar answered Nov 15 '22 20:11

MSalters


The types you mention are keywords. Keywords are not identifiers and therefore cannot belong to scopes or namespaces. During parsing of the program , keywords are found at an earlier stage than identifiers.

Changing the namespace of the program entry point (::main currently) would mean all linkers everywhere have to be updated and so I doubt there would be any support for such a move. Also it would go against the principle that std is for the standard library and not for user code, whereas the user writes the code that goes in main.

like image 44
M.M Avatar answered Nov 15 '22 21:11

M.M