gpt4 book ai didi

c++ - C++14 中 main() 的合法定义

转载 作者:IT老高 更新时间:2023-10-28 21:42:21 26 4
gpt4 key购买 nike

我能找到的 C++14 的最后一个草稿说,关于 main() [3.6.1]:

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. All implementations shall allow both

— a function of () returning int and

— a function of (int, pointer to pointer to char) returning int

和(第 5 段)

If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;

这是否意味着以下所有的都是合法的 C++14 最小程序?如果没有,为什么不呢?

  1. auto main() -> int {}
  2. auto main() { return 0; }
  3. auto main() {}

最佳答案

  1. 合法,第二个和后者不合法,原因如下:

  2. 由于CWG 1669被接受,因此无法推断主函数的返回类型,标准将改写为:

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

    这进入了 n4140。更多信息:http://wg21.cmeerw.net/cwg/issue1669

  3. 同上

关于c++ - C++14 中 main() 的合法定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26470912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com