开发者

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

开发者 https://www.devze.com 2022-12-09 15:32 出处:网络
This question already has answers here: What should main() return in C and C++? (19 answers) Closed 4 years ago.
This question already has answers here: What should main() return in C and C++? (19 answers) Closed 4 years ago.

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 guarant开发者_JAVA技巧eed to work on a standards conforming compiler?


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. ]


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.


The argv pointers shouldn't be const char* const because the program is allowed to change the buffers.


As far as I can see from reading the standard, you're being non-standards-compliant. But I can't imagine a compiler that wouldn't let you do this. As in, it'd take more work from the compiler to specifically ban an edge case that is mostly harmless and very obscure.


This may not work if the compiler uses name mangling for main. It's a C++ function after all. Hence, the linker will be looking for two particular "manglings". Your definition would have another mangled name.

Note that main is special (doesn't overload, not callable) and might not require name mangling at all.


You might be illegal by the standard, but most runtimes don't really care. They'll just push an integer for argc and a pointer for argv, call your main, and hope you parse them right. So, in your purview, "guaranteed to work" is debatable as the loader really doesn't care what you've declared the arguments as.

If it builds, main will get called. How you parse the arguments is up to you. I should clarify that this is highly platform-specific, as is nearly this entire question.

That said, why?


ISO/IEC 9899:TC3

Section 5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;9) or in some other implementation-defined manner.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号