views:

608

answers:

7

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?

+1  A: 

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.

pavpanchekha
Amen. your code MAY have issues compiling on a hypothetical compiler that 100% follows the standard. But most compilers do not, and this edge case is probably one of those where they allow what is technically an illegal behavior.
DVK
+1  A: 

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

jeffamaphone
**Only** the strings, not the pointers to the strings.
Jed Smith
That's the point. I don't want to change anything!
bh9042
Then don't change anything.
jeffamaphone
+2  A: 

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?

Jed Smith
1. argc is guaranteed to be non-negative, I want to see an unsigned somewhere in there... ;) 2. I want the extra safety net that const provides against stupid mistakes. Like I responded to jeffamaphone, even though I'm allowed to change argv, I don't want or need to.
bh9042
`main` predates C++, and even the time of `unsigned`. Regardless of what you want, `main` has been declared this way for a long, long time. You really need the compiler to protect you from modifying `argv`? If you don't have that `const` there are you accidentally going to replace all the '-' with '/' in your `argv` strings? You're not selling me on why this is needed.
Jed Smith
I think it's a valid point that allowing programmers to specify assumptions and constraints through modifiers like `unsigned` and `const` should be allowed anywhere. If it's good for user-defined functions, I don't see why it wouldn't be good for `main()` too.
Andrew Coleson
afaics, there is nothing platform specific in this question.
Johannes Schaub - litb
+8  A: 

The C++98 standard says in section 3.6.1.2

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.

Motti
Ah, I missed the "type" part when I read it.
bh9042
+2  A: 

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.

mh
+1 for providing a useful answer instead of everyone else's "I don't see why you'd do this."
Andrew Coleson
A: 

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.

MSalters
A: 

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.

Kedar