Kaz Kylheku wrote:[color=blue]
> Greg Comeau wrote:[color=green]
> > In article <1131299404.895958.77850@o13g2000cwo.googlegroups. com>,
> > Neelesh <neelesh.bodas@gmail.com> wrote:[color=darkred]
> > >Hi all,
> > >it is strange that the following code compiles and runs (tested with
> > >g++ 3.4.2)
> > >
> > >#include <iostream>
> > >using namespace std;
> > >
> > >int main(int a, int b, int c, int d, int e)
> > >{
> > >cout << "hello " << endl;
> > >}
> > >
> > >Is C++ lenient about the prototype of main? Does it say anything if the
> > >prototype is not one of those standard argc-argv-env kinds?
> > >Is the program expected to run in such cases or this is simply by luck
> > >that it runs properly?[/color]
> >
> > As I recall that particular case is implementation-defined. Check out
> >
http://www.comeaucomputing.com/techtalk/#voidmain[/color]
>
> The program's behavior is undefined, because implementations are not
> required to support that form of main.[/color]
Implementation-defined behavior is not undefined behavior. So without
knowing anything about the implementation, it is not possible to
conclude whether this program's behavior is undefined or not. It is
possible to determine whether the program's behavior is defined for a
given compiler, because all implementation-defined behavior must be
documented. So in this case, it is necessary only to consult the
"implementation-defined behavior" section of the compiler's
documentation to find out what it says about arguments to main.
[color=blue]
> A program that uses a feature for which the support is
> implementation-defined in fact invokes undefined behavior.[/color]
No, it invokes the behavior that the implementation, and not the
Standard, has defined for it.
[color=blue]
> I would say that the definition
>
> int main(int a, int b, int c, int d, int e) { /* ... */ }
>
> is /worse/ than one returning void! The reason is that no diagnostic is
> required! A diagnostic is required if the return type is other than
> int. (In that case, the C++ implementation can still translate and
> execute the program anyway, but its behavior is undefined).
>
> Anything other than int main() or int main(int, char **) runs by dumb
> luck, or by means of being supported as an extension, which has nothing
> to do with the standard other than being allowed with or without a
> diagnostic, depending on whether the return type is int.[/color]
The C++ standard specifically leaves some questions - such as the size
of a byte or additional declarations of main - up to each
implementation to decide on its own. It does so because it recognizes
that a "one-size-fits-all" approach is not always practicable for all
types of processors and OS'es. Instead it is better to have each
implementation be responsible for deciding these types of issues and
for documenting the decision. So any C++ program whose behavior is
defined by either the Standard or by the implementation is a valid and
perfectly acceptable C++ program.
Greg