Two questions about the principal

A. Is the following attempt valid to define the entry point ‘main’ of a C++ standalone program?

namespace{
 extern int main(){return 0;}
}

As far as I understand, it satisifies all the criteria about ‘main’ in the C++ standard (external linkage, available in global namespace because of the implicit using directive).

So is this program ill-formed and why? Any reference to the Standard?

B. I have gone through dicussions on EXIT_FAILURE and EXIT_SUCCESS but unable to conclude if EXIT_SUCCESS should always be 0. This is because as per the Standard leaving out a return statement in ‘main’ is equivalent to return 0. So I guess, EXIT_SUCCESS should be always 0.

$18.3 – “If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.”

A. Is the following attempt valid to define the entry point 'main' of a C++ standalone program?

No. The C++ standard says “A program shall contain a global function called main
” (§3.6.1/1). In your program, the main
function is not in the global namespace; it is in an unnamed namespace.

The implicit using directive only allows names from the unnamed namespace to be looked up and used in the enclosing namespace; it does not add those names to the enclosing namespace. Specifically, “a using-directive
does not add any members to the declarative region in which it appears” (§7.3.4/1).

Is this program ill-formed and why?

The program is not necessarily ill-formed. There is no rule against having a function named main
in a namespace other than the global namespace; such a function is just not the
main
function. namespace { int main(); }
and int main()
are two different functions, and a well-formed program can have both of them.

Note that if your program does not have a main
function in the global namespace, then the program is ill-formed (because, as stated above, a program in a hosted environment must have a main function).

B. I have gone through dicussions on EXIT_FAILURE
and EXIT_SUCCESS
but unable to conclude if EXIT_SUCCESS
should always be 0
.

There is no requirement that EXIT_SUCCESS
expand to 0
. The C standard simply says that in
,

The macros defined are… EXIT_FAILURE
and EXIT_SUCCESS
which expand to integer constant expressions that can be used as the argument to the exit
function to return unsuccessful or successful termination status, respectively, to the host environment (C99 §7.20/3).

(Since those two macros are defined in
, the C standard contains the specification for them).

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Two questions about the principal

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录