Unlike everything is standard
ized 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.
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.
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? - 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.
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.
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.
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.
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 #include
s.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With