将自动返回类型扣除工作为主? [英] Will automatic return type deduction work for main?

查看:174
本文介绍了将自动返回类型扣除工作为主?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以为C ++ 1y(C ++ 14)中的主函数执行以下操作:

Will I be able to do the following for the main function in C++1y (C++14):

auto main()
{
    // ...
}

即使我们不需要使用显式的 return 0; ,返回类型也会自动为 int >

So will the return type automatically be int even though we don't need to use an explicit return 0;?

推荐答案

不会, C ++ 14标准草案N3690的段落7.1.6.4/10规定:

No, it won't be allowed. Paragraph 7.1.6.4/10 of the C++14 Standard Draft N3690 specifies:


如果一个函数声明返回类型使用占位符类型没有 return 语句,返回
类型被推断为从 return 语句中没有操作数在函数
body的结束大括号。 [...]

If a function with a declared return type that uses a placeholder type has no return statements, the return type is deduced as though from a return statement with no operand at the closing brace of the function body. [...]

这意味着省略了 return code> main()会使其类型 void

This means that omitting a return statement in main() would make its type void.

第3.6.1 / 5段介绍的关于流出 main()结束的特殊规则指定:

The special rule introduced by paragraph 3.6.1/5 about flowing off the end of main() specifies:


[...]如果控制到达 main 的结束
,而不会遇到返回语句,效果是执行

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

return 0;


这个词语表示效果程序的执行与存在 return 0 相同,而不是 return 语句

The wording says that the "effect" during the execution of the program is the same as though a return 0 was present, not that a return statement will be added to the program (which would affect type deduction according to the quoted paragraph).

编辑

有一个缺陷报告礼貌)

There is a Defect Report for this (courtesy of Johannes Schaub):


$ b

建议的决议(2013年11月)

Proposed resolution (November, 2013):

更改3.6.1 [basic.start.main]第2段如下:

Change 3.6.1 [basic.start.main] paragraph 2 as follows:

实现不应预定义main函数。此功能不应重载。它应具有声明的类型为int的返回类型,否则其类型是实现定义的。 所有实施 实施应允许<$ c $的函数

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


  • int )的函数()返回 int / code>,指向 char 的指针指向 int

  • a function of () returning int and
  • a function of (int, pointer to pointer to char) returning int

作为类型...

这篇关于将自动返回类型扣除工作为主?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆