Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the arguments of main's signature in C++ have the unsigned and const qualifiers? [duplicate]

The standard explicitly states that main has two valid (i.e., guaranteed to work) signatures; namely:

int main(); int main(int, char*[]); 

My question is simple, would something like the following be legal?

int main(const unsigned int, const char* const* argv); 

My tests say 'yes', but I'm unsure of the answer because am I not overloading main by changing int to unsigned int as well as the non top-level const-ness of argv? If I am, then that's clearly prohibited.

So, are these modifications guaranteed to work on a standards conforming compiler?

like image 266
bh9042 Avatar asked Oct 25 '09 18:10

bh9042


2 Answers

The C++98 standard says in section 3.6.1 paragraph 2

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 the following definitions of main: int main() and int main(int argc, char* argv[])

So it's not mandated by the standard that the env accepting main is acceptable but it is permissible.


Because this is referred to often, here is the previous paragraph exempting freestanding environments from anything but documenting their behavior:

A program shall contain a global function called main, which is the designated start of the program. It is implementation defined whether a program in a freestanding environment is required to define a main function. [Note: in a freestanding environment, startup and termination is implementation defined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. ]

like image 92
Motti Avatar answered Oct 02 '22 18:10

Motti


You must use one of the standard-conformant signatures to be standard-conformant.

I fully understand why you want to do it your way. The best way is to write your own function myMain() or whatever with the signature you want and call it from main(), including the required casts.

like image 36
mh. Avatar answered Oct 02 '22 18:10

mh.