12

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.

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
deadLock
  • 390
  • 6
  • 16

4 Answers4

12

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.

VLL
  • 9,634
  • 1
  • 29
  • 54
11

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
3

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.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Using `std::main` instead of `main` would require changing the startup code (the assembly code that actually gets run by the OS, and calls `main`); it would generally not require any linker changes. – Pete Becker Mar 09 '20 at 13:16
0

As far as I know. Int is a primitive (primary) data type. Primary data types are already defined in the programming languages also called primitive or in-built data types. "int" data type is one of them. In some of the languages, you can see some of the byte, short, int, long, char, float, double or boolean keywords used as themselves.

Hope I was helpful.

Catastrophe
  • 322
  • 3
  • 12