Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the usage of main without a return type phased out in C++11?

Tags:

c++

c++11

Stephen Prata in his book C++ Primer Plus [p 31] says :

Many existing programs use the classic C function header instead:

main() // original C style

Under classic C, omitting the return type is the same as saying that the function is type int. However, C++ has phased out that usage.

However The C++11 draft 3.6.1->2 says

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.

Test Result

$ g++ -Werror=pedantic MainCheck.cpp -o MainCheck
MainCheck.cpp:3:6: error: ISO C++ forbids declaration of ‘main’ with no type [-Werror=pedantic]
 main()
$ # also means g++ don't conform to the standard

confirms that what Mr. Prata said is true when it comes to C++ standard.

Is there a clause in the C++11 draft that discourages the use of :

main() // that is without return type.

Is

It shall have a return type of type int

itself such a clause?

like image 599
sjsam Avatar asked Jun 17 '16 05:06

sjsam


4 Answers

See also What should main() return in C and C++?

ISO/IEC 14882:1998 contains:

§7 Declarations

¶7 Only in function declarations for constructors, destructors, and type conversions can the decl-specifier-seq be omitted.78)

and footnote 78 says:

The “implicit int” rule of C is no longer supported.

The same statements are in ¶9 and footnote 89 in the C++11 standard.

So, main() declared without the return type has never been a part of standard C++, but it was allowed roughly up until C++98 standard was created (and probably a bit longer for reasons of backwards compatibility).

If you look in Stroustrup's "Design and Evolution of C++" (published 1994), §2.8 The C Declaration Syntax says:

Allowing the type specifier to be omitted (meaning int by default) also led to complications. … The negative reaction to changes in this area from users was very strong. … I backed out the change. I don't think I had a choice. Allowing that implicit int is the source of many of the annoying problems with the C++ grammar today. … Finally, ten years later, the C++ ANSI/ISO standard committee has decided to deprecate implicit int. That means we may get rid of it in another decade or so.

like image 103
Jonathan Leffler Avatar answered Nov 15 '22 20:11

Jonathan Leffler


Implicit int types were disallowed in all C++ standards, i.e., it wasn't allowed in C++98. It isn't anything new in C++11. There wasn't any exception for main() with respect to the declaration of the function. Also, the implicit int rule applied to all declarations not just to main().

The relevant clause in the C++ standard is 7 [dcl.dcl] paragraph 11:

Only in function declarations for constructors, destructors, and type conversions can the decl-specifier-seq be omitted.94

94) The “implicit int” rule of C is no longer supported.

I don't have easy access to the C++98 standard right now but C++03 definitely has the same statement. The only difference is that it is in paragraph 7 and the footnote is "79" instead of "94".

C's implicit int rule was part of C++ for a while prior to the first standard but got removed at some point. Compilers may accept programs omitting the type as an extension but I think they are required to emit a diagnostic. Whether that means much is a separate question as it is long established that writing a single \r at the start of a line would meet that requirement.

What is there and what will remain is the odd exception that program execution can flow off the end of main() without a return statement: it is assumed that mandating a return statement in main() would break existing code.

The statement

It shall have a return type of type int

doesn't state anything about how the function acquires its return type. It only states what the return type shall be.

like image 24
Dietmar Kühl Avatar answered Nov 15 '22 19:11

Dietmar Kühl


This is what the working draft says about the main function.
Excerpts follow.

  • 3.6.1/2

An implementation shall not predefine the main function. This function shall not be overloaded. Its type shall have C++ language linkage and it shall have a declared return type of type int, but otherwise its type is implementation-defined. An implementation shall allow both

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

as the type of main

  • 3.6.1/5:

A return statement in main has the effect of leaving the main function [...] If control flows off the end of the compound-statement of main, the effect is equivalent to a return with operand 0

So, the standard says that the main function shall have one of the following types:

  • int()

  • int(int, char **)

Being the main function a function, I guess it follows also the rules in 8.3.5.
The question would become thus: can I omit the return type from a function definition? Does the standard allow me to do that?
The answer is no, indeed.

like image 28
skypjack Avatar answered Nov 15 '22 21:11

skypjack


It is not required by the standard for the main to explicitly return. If return statement is missing, compiler simply puts:

return 0;

at the end. This rule doesn't apply to other functions.

In C, default return type of any function was int if not provided by programmer, for example:

get() // int assumed
{
    return 10;
}

C++ standard requires that you must specify the return type. Above code will not compile in C++ compiler. C90 allowed implicit int, C99 disallows implicit int.

like image 39
Ajay Avatar answered Nov 15 '22 20:11

Ajay