Is main() overloaded in C++?

后端 未结 6 1695
再見小時候
再見小時候 2020-11-29 01:51

2 valid versions of main() exist in C++:

int main()  // version 1
int main(int argc, char **argv)  // version 2

B

6条回答
  •  死守一世寂寞
    2020-11-29 02:16

    Section 3.6.1.2 of both C++ Standard 1998 and 2003 editions states:

    An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.

    Further,

    The ISO C++ Standard (ISO/IEC 14882:1998) specifically requires main to return int. It has an explicit "shall" constraint upon well-formed programs.

    Section § 3.6.1 ¶ 2:

    It shall have a return type of int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

    int main() { /* … */ }
    

    and

    int main(int argc, char* argv[]) { /* … */ }
    

    So both versions of main are allowed by the standard and which one to use is left as an implementation preference of the programmer.

提交回复
热议问题