Two main questions

and. Is the following attempt valid for determining the entry point of a "main" stand-alone C ++ program?

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

As far as I understand, it satisfies all the criteria of "main" in the C ++ standard (external communication available in the global namespace due to the implicit usage directive).

So this program is poorly formed and why? Any reference to the standard?

Q. I went through discussions on EXIT_FAILURE and EXIT_SUCCESS, but could not complete if EXIT_SUCCESS should always be 0. This is because, according to the standard leaving the return statement in "main", returning 0 is equivalent. So, I think, EXIT_SUCCESS must always be 0.

$ 18.3 - "If the status is zero or EXIT_SUCCESS, a form is returned that defines the implementation, successful completion of the state."

+3
source share
3 answers

and. Is the following attempt valid for determining the entry point of a "main" stand-alone C ++ program?

No. The C ++ standard states: “A program must contain a global function called main” (§3.6.1 / 1). In your program, a function is mainnot part of the global namespace; it is in an unnamed namespace.

; . , " using - , " (§7.3.4/1).

?

. main , ; main. namespace { int main(); } int main() - , .

, main , (, , ).

. dicussions EXIT_FAILURE EXIT_SUCCESS, , EXIT_SUCCESS 0.

, EXIT_SUCCESS 0. C , <stdlib.h>,

... EXIT_FAILURE EXIT_SUCCESS, , exit , , (C99 §7.20/3).

( <cstdlib>, C ).

+1

namespace { } - , - -, . extern, extern "C", , main , , . , - , . , , :-). - ? - ?

, EXIT_SUCCESS EXIT_FAILURE. , main exit() , . 0 , EXIT_SUCCESS, 0 "". 0 ... , , "" . - , 0, - , . - : (, Linux) , 8 , 256 0. , 0.

+2

. Visual ++ 2010, :

1 LNK1561:

++ main (,:: main) ( ), () - , .

, , C : EXIT_SUCCESS ( ) EXIT_FAILURE. .

.

0

Source: https://habr.com/ru/post/1763713/


All Articles