Topic: Void main() -- 1 Declaration of `main


Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/06/30
Raw View
shepherd@debussy.sbi.com (Marc Shepherd) writes:

>bhurt@subzero.winternet.com (Brian Hurt) writes:
>>One other possibilty occurs: writing a "compatability library" for
>>windows, I can see code like:

>>   extern int main (int, char**);
>>   int WinMain (...) {
>>      // ...
>>      retval = main(argc, argv);
>>      // ...
>>      return retval;
>>    }
>
>The above code would not be standard-conforming, since it includes
>what amounts to a recursive call of 'main'.

So long as the Windows compilers accept the code, it doesn't really matter
if it is not standard conforming - the code is for a compatibility library,
after all.  On other systems, you wouldn't want or need to use it.

--
Fergus Henderson
fjh@cs.mu.oz.au
http://www.cs.mu.oz.au/~fjh
"The unexamined life is not worth living" - Socrates.





Author: bhurt@subzero.winternet.com (Brian Hurt)
Date: 1995/06/13
Raw View
chris@alofi.etca.fr (Christian Millour) writes:

>In article <3r346t$io1@engnews2.Eng.Sun.COM>, clamage@Eng.Sun.COM (Steve Clamage) writes:
>|> JdeBP@jba.co.uk (Jonathan de Boyne Pollard) writes:
>|>
>|> >Is it legal for the programmer to *declare* `main' ?
>|>
>|> >In other words, should the following program compile ?
>|>
>|> >      int main ( int, char ** ) ;
>|> >      int main ( int, char ** ) { return 0 ; }
>|>
>|>
>|> That said, why would you want to declare main? You are not allowed to
>|> call the funtion nor to take its address, so declaration that isn't
>|> also a definition doesn't serve any purpose.
>|>
One other possibilty occurs: writing a "compatability library" for
windows, I can see code like:
   extern int main (int, char**);
   int WinMain (...) {
      // ...
      retval = main(argc, argv);
      // ...
      return retval;
    }
Brian Hurt.
Who is being forceably restrained from making snide comments about Bill
Gates and Microsoft, who made the above code nessecary...






Author: JdeBP@jba.co.uk (Jonathan de Boyne Pollard)
Date: 1995/06/14
Raw View
Brian Hurt (bhurt@subzero.winternet.com) wrote:
: One other possibilty occurs: writing a "compatability library" for
: windows, I can see code like:
:    extern int main (int, char**);
:    int WinMain (...) {
:       // ...
:       retval = main(argc, argv);
:       // ...
:       return retval;
:     }
: Brian Hurt.
: Who is being forceably restrained from making snide comments about Bill
: Gates and Microsoft, who made the above code nessecary...

Except that that disobeys the rule against calling `main'.

By far the better approach (and I see no reason why an implementor could
not do this) is to have an additional type for `main' allowed by the
implementation :

    int main ( int, char **, HINSTANCE, HINSTANCE /* ... and so on ... */ )





Author: shepherd@debussy.sbi.com (Marc Shepherd)
Date: 1995/06/14
Raw View
In article 803074836@winternet.com, bhurt@subzero.winternet.com (Brian Hurt) writes:
>One other possibilty occurs: writing a "compatability library" for
>windows, I can see code like:
>   extern int main (int, char**);
>   int WinMain (...) {
>      // ...
>      retval = main(argc, argv);
>      // ...
>      return retval;
>    }
>Brian Hurt.
>Who is being forceably restrained from making snide comments about Bill
>Gates and Microsoft, who made the above code nessecary...
>

The above code would not be standard-conforming, since it includes
what amounts to a recursive call of 'main'.

(But Brian Hurt is right about 'WinMain' being a Microsoft botch.)


---
Marc Shepherd
Salomon Brothers Inc
shepherd@schubert.sbi.com The opinions I express are no one's but mine!






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/06/07
Raw View
JdeBP@jba.co.uk (Jonathan de Boyne Pollard) writes:

>First quibble with [basic.start.main] :

>Is it legal for the programmer to *declare* `main' ?

>In other words, should the following program compile ?

>      int main ( int, char ** ) ;
>      int main ( int, char ** ) { return 0 ; }

Interesting question, not specifically addressed. Since it is otherwise
valid to declare any function, and since the given definition must
be accepted by the compiler, I would say that the program is valid.

That said, why would you want to declare main? You are not allowed to
call the funtion nor to take its address, so declaration that isn't
also a definition doesn't serve any purpose.

>From experience, four out of the five C++ compilers that I tried *did*
>compile the above program to a valid executable.  The fifth C++ compiler
>compiled, but could not link the resulting executable.  It turned out that
>the declaration of `main' turns off the internal compiler magic for `main'
>and causes it to be treated as if it were a normal function.

That last sounds like a compiler bug.


>The point is, that [basic.start.main] doesn't say that you may not have a
>declaration of `main' apart from its definition.  I believe that it should.
>For one thing, such a prohibition prevents `main' from being declared
>outside of the translation unit in which it is defined, thus denying the
>programmer one way that he could take the address of `main', or call it.

But the result of doing either of those is undefined. What good is
the guaranteed ability to declare the function if you can't predict
what will happen if you take advantage of the declaration?

Please note that I am discussing what the language definition
promises. An implementation that allowed you to call main or to
take its address as a language extension would presumably also
allow you to declare it.
--
Steve Clamage, stephen.clamage@eng.sun.com





Author: chris@alofi.etca.fr (Christian Millour)
Date: 1995/06/07
Raw View
In article <3r346t$io1@engnews2.Eng.Sun.COM>, clamage@Eng.Sun.COM (Steve Clamage) writes:
|> JdeBP@jba.co.uk (Jonathan de Boyne Pollard) writes:
|>
|> >First quibble with [basic.start.main] :
|>
|> >Is it legal for the programmer to *declare* `main' ?
|>
|> >In other words, should the following program compile ?
|>
|> >      int main ( int, char ** ) ;
|> >      int main ( int, char ** ) { return 0 ; }
|>
|> Interesting question, not specifically addressed. Since it is otherwise
|> valid to declare any function, and since the given definition must
|> be accepted by the compiler, I would say that the program is valid.
|>
|> That said, why would you want to declare main? You are not allowed to
|> call the funtion nor to take its address, so declaration that isn't
|> also a definition doesn't serve any purpose.
|>

Huuu, how about a friend declaration ? e.g., the MotifApp framework in
"Object-Oriented Programming with C++ and OSF/Motif" by Douglas Young
provides its own main, as

// Main.C
#include "Application.h"
int main ( int argc, char **argv ) {
  ...
  theApplication->initialize ( &argc, argv );
  ...
}

with

// Application.h
class Application ... {
friend int main(int,char**);
  ...
protected:
  virtual void initialize(int*, char**);
  ...
};
extern Application* theApplication;



--chris@etca.fr

|> --
|> Steve Clamage, stephen.clamage@eng.sun.com





Author: JdeBP@jba.co.uk (Jonathan de Boyne Pollard)
Date: 1995/06/07
Raw View
Steve Clamage (clamage@Eng.Sun.COM) wrote:
: That said, why would you want to declare main? You are not allowed to
: call the funtion nor to take its address, so declaration that isn't
: also a definition doesn't serve any purpose.

This is exactly what I said in the last paragraph of the original message.

: >From experience, four out of the five C++ compilers that I tried *did*
: >compile the above program to a valid executable.  The fifth C++ compiler
: >compiled, but could not link the resulting executable.  It turned out that
: >the declaration of `main' turns off the internal compiler magic for `main'
: >and causes it to be treated as if it were a normal function.

: That last sounds like a compiler bug.

Why ?  Surely if you declare `main' outside of its definition you are in
the realms of "implementation defined" ?  By my proposal, the declaration
would be illegal anyway, and the point would be moot.

: >The point is, that [basic.start.main] doesn't say that you may not have a
: >declaration of `main' apart from its definition.  I believe that it should.
: >For one thing, such a prohibition prevents `main' from being declared
: >outside of the translation unit in which it is defined, thus denying the
: >programmer one way that he could take the address of `main', or call it.

: But the result of doing either of those is undefined. What good is
: the guaranteed ability to declare the function if you can't predict
: what will happen if you take advantage of the declaration?

You didn't read what I wrote correctly, because you are addressing the exact
opposite of what I actually said.  Here are the first two sentences again,
this time with the operative words emphasised for clarity :

> The point is that [basic.start.main] DOESN'T say that you MAY NOT have a
> declaration of `main' apart from its definition.  I believe that it SHOULD.

Clear what I'm advocating now ?  The final sentence in my original
probably makes more sense to you now, yes ?

: Please note that I am discussing what the language definition
: promises. An implementation that allowed you to call main or to
: take its address as a language extension would presumably also
: allow you to declare it.

What I'm saying is that as it stands, the standard doesn't say one way or
another about the behaviour when you declare `main' outside of its
definition, and I'm advocating tidying that up by prohibiting any such
declaration.  As you yourself said (and indeed as I said in the original
message) the only use for such a declaration would be to take the address
of `main' or call it, both of which actions are prohibited.

Clearly we want `main' to be as much like a normal function as possible,
and that normally means that we can declare it outside of its definition.

However, since there are strictly defined things that the programmer cannot
do with `main' that can be done to normal functions, we don't want glaring
loopholes such as being able to declare `main' outside of its definition if
it is illegal to use such a declaration in any way.  This is especially so
when we find that implementors are relying on the fact that `main' is a
distinguished function in order to imbue it with the "magic" properties
necessary for the implementation.





Author: JdeBP@jba.co.uk (Jonathan de Boyne Pollard)
Date: 1995/06/06
Raw View
First quibble with [basic.start.main] :

Is it legal for the programmer to *declare* `main' ?

In other words, should the following program compile ?

      int main ( int, char ** ) ;
      int main ( int, char ** ) { return 0 ; }