Re: Big Deal with returning int

Discussion in 'C++' started by Kelsey Bjarnason, Jun 29, 2003.

  1. [snips]

    On Sun, 29 Jun 2003 07:18:14 +0100, John Harrison wrote:

    >> I have seen so many pointing out "main" should explicitly return "int".
    >> Beside, the language spec or committee, or some guru said so, what is a

    > BIG
    >> deal with it ?

    >
    > I really have no idea. As you say it is pointed out time and time again
    > here, but no-one seems to be able to explain why. You'd think that with the
    > frequencey that this is pointed out, someone would also supply an
    > explaination.
    >
    > Furthermore even though main must be declared to return an int, you don't
    > actually have to return one! Its the only function that gets this special
    > treatment. To me its bizarre, and since I have never seen an explaination I
    > assume its has none.
    >
    >> What difference does it make if "void main" rather than "int
    >> main" ?

    >
    > Personally I wouldn't take the presence of void main as evidence against a
    > books quality, I would look to more important issues. void main is a
    > completely trivial issue which like many trivial issues (indentiation style
    > for instance) gets some people hot under the collar.


    Except that void main isn't a trivial issue; worse, by getting such a
    _simple_ issue wrong, the authors instantly lose any credibility they may
    have had when it comes to dealing with hard things.

    As to _why_ int main is important...

    Well, first, that's how the language is defined, which means using int
    main is the only assured way to have things work. If one only ever uses a
    compiler which accepts void main, one may never notice this directly, but
    C++ isn't about code that runs on one compiler, it's about portable code.

    That said... here's a possible implementation, in terms of the details of
    the startup code:

    First, the "init" module is run, which sets up various things. It calls
    main, which does its thing, returns, and the init code rips the return
    value and hands it back to the universe.

    However... this implementation is a little odd; it doesn't use a stack.
    Instead, space is reserved at the head of the function. Thus, the init
    code looks something like this:

    .init proc
    load rega <- .main ; address of main
    add rega, 4 ; size of int
    sub rega ; call main

    Now, since you've told your compiler that main returns void, it compiles
    as you told it to. Thus, the code looks lke this:

    .main proc
    ; code starts here

    Oops, we have a problem - there's _supposed_ to be space there for a
    return value - an int. The startup code *knows* main returns an int, so
    it automatically adds the size of one to the jump address. You, however,
    have goofed and produced a broken main, so what is actually executed is
    not the start of the code, but whatever happens to be 4 bytes in from that
    point, which is almost guaranteed _not_ to do the right thing. Never mind
    your app failing to return a proper value, your app may not run _at all_,
    crashing at or very near to this point. Or it may run, but executing a
    competely different set of instructions, with unpredictable results.

    Fundamentally, it comes down to this: void main introduces a point of
    failure which is both completely avoidable and _trivial_ to avoid; why,
    then, risk failure?

    --
    http://rkc.silversapphire.com
    Managed Migration from Windows to Linux
    Kelsey Bjarnason, Jun 29, 2003
    #1
    1. Advertising

  2. "Kelsey Bjarnason" <> wrote in message
    news:p...
    > [snips]
    >
    > On Sun, 29 Jun 2003 07:18:14 +0100, John Harrison wrote:
    >
    > >> I have seen so many pointing out "main" should explicitly return "int".
    > >> Beside, the language spec or committee, or some guru said so, what is a

    > > BIG
    > >> deal with it ?

    > >
    > > I really have no idea. As you say it is pointed out time and time again
    > > here, but no-one seems to be able to explain why. You'd think that with

    the
    > > frequencey that this is pointed out, someone would also supply an
    > > explaination.
    > >
    > > Furthermore even though main must be declared to return an int, you

    don't
    > > actually have to return one! Its the only function that gets this

    special
    > > treatment. To me its bizarre, and since I have never seen an

    explaination I
    > > assume its has none.
    > >
    > >> What difference does it make if "void main" rather than "int
    > >> main" ?

    > >
    > > Personally I wouldn't take the presence of void main as evidence against

    a
    > > books quality, I would look to more important issues. void main is a
    > > completely trivial issue which like many trivial issues (indentiation

    style
    > > for instance) gets some people hot under the collar.

    >
    > Except that void main isn't a trivial issue; worse, by getting such a
    > _simple_ issue wrong, the authors instantly lose any credibility they may
    > have had when it comes to dealing with hard things.


    Maybe they are just concentrating on the hard things. If they get the hard
    things right then I wouldn't worry about the use of void main, that would be
    perverse. And to dismiss all books that use void main is prejudiced. Sure
    void main is an error, but I would still maintain its a trivial one.

    >
    > As to _why_ int main is important...
    >
    > Well, first, that's how the language is defined, which means using int
    > main is the only assured way to have things work. If one only ever uses a
    > compiler which accepts void main, one may never notice this directly, but
    > C++ isn't about code that runs on one compiler, it's about portable code.
    >


    That just restates what I said earlier, int main is standard, and we should
    use it for that reason. It doesn't explain why void main is non-standard.

    > That said... here's a possible implementation, in terms of the details of
    > the startup code:
    >
    > First, the "init" module is run, which sets up various things. It calls
    > main, which does its thing, returns, and the init code rips the return
    > value and hands it back to the universe.
    >
    > However... this implementation is a little odd; it doesn't use a stack.
    > Instead, space is reserved at the head of the function. Thus, the init
    > code looks something like this:
    >
    > .init proc
    > load rega <- .main ; address of main
    > add rega, 4 ; size of int
    > sub rega ; call main
    >
    > Now, since you've told your compiler that main returns void, it compiles
    > as you told it to. Thus, the code looks lke this:
    >
    > .main proc
    > ; code starts here
    >
    > Oops, we have a problem - there's _supposed_ to be space there for a
    > return value - an int. The startup code *knows* main returns an int, so
    > it automatically adds the size of one to the jump address. You, however,
    > have goofed and produced a broken main, so what is actually executed is
    > not the start of the code, but whatever happens to be 4 bytes in from that
    > point, which is almost guaranteed _not_ to do the right thing. Never mind
    > your app failing to return a proper value, your app may not run _at all_,
    > crashing at or very near to this point. Or it may run, but executing a
    > competely different set of instructions, with unpredictable results.


    There is no reason that the compiler couldn't perform the necessary
    tranformation to void main, silently adding space for an int return. I think
    you are confusing the implementaion of a language with its definition. I
    can't see any reason why

    void main()
    {
    }

    couldn't have been defined as entirely equivalent to

    int main()
    {
    return 0;
    }

    (for instance). Doing so would have validated common practise (which is no
    bad thing), but it wasn't for reasons I still don't understand.

    >
    > Fundamentally, it comes down to this: void main introduces a point of
    > failure which is both completely avoidable and _trivial_ to avoid; why,
    > then, risk failure?


    Are you really saying that allowing void main would put such a strain on
    compilers that they could not produce correct code? I don't think so.

    >
    > --
    > http://rkc.silversapphire.com
    > Managed Migration from Windows to Linux
    >


    john
    John Harrison, Jun 29, 2003
    #2
    1. Advertising

  3. Kelsey Bjarnason

    Rolf Magnus Guest

    John Harrison wrote:

    > Maybe they are just concentrating on the hard things. If they get the
    > hard things right then I wouldn't worry about the use of void main,
    > that would be perverse. And to dismiss all books that use void main is
    > prejudiced. Sure void main is an error, but I would still maintain its
    > a trivial one.


    I see it a bit different. If an author uses void main, that shows that
    he doesn't know C++ very well. This gives not much trust in the rest of
    the book.

    > There is no reason that the compiler couldn't perform the necessary
    > tranformation to void main, silently adding space for an int return. I
    > think you are confusing the implementaion of a language with its
    > definition. I can't see any reason why
    >
    > void main()
    > {
    > }
    >
    > couldn't have been defined as entirely equivalent to
    >
    > int main()
    > {
    > return 0;
    > }
    >
    > (for instance). Doing so would have validated common practise (which
    > is no bad thing), but it wasn't for reasons I still don't understand.


    It's quite interesting that you call "void main" common practise, since
    it actually never was allowed. So you think the rule should be changed,
    because nobody follows it anyway?

    >> Fundamentally, it comes down to this: void main introduces a point of
    >> failure which is both completely avoidable and _trivial_ to avoid;
    >> why, then, risk failure?

    >
    > Are you really saying that allowing void main would put such a strain
    > on compilers that they could not produce correct code?


    Actually, that may be the case. Or a compiler may choose to not produce
    any code at all since the source has an error.

    > I don't think
    > so.
    >
    >>
    >> --
    >> http://rkc.silversapphire.com
    >> Managed Migration from Windows to Linux
    >>

    >
    > john
    Rolf Magnus, Jun 29, 2003
    #3
  4. >
    > It's quite interesting that you call "void main" common practise, since
    > it actually never was allowed. So you think the rule should be changed,
    > because nobody follows it anyway?


    You only have to read here for a while to see that it is common practise.

    >
    > >> Fundamentally, it comes down to this: void main introduces a point of
    > >> failure which is both completely avoidable and _trivial_ to avoid;
    > >> why, then, risk failure?

    > >
    > > Are you really saying that allowing void main would put such a strain
    > > on compilers that they could not produce correct code?

    >
    > Actually, that may be the case. Or a compiler may choose to not produce
    > any code at all since the source has an error.
    >


    That's a gem of a circular argument. void main is illegal, therefore the
    compiler needn't compile it, therefore we can't allow void main.

    What I am saying (in contrast to Kelsey) is that there is no technical
    reason that void main could not be part of standard C++. What ever reasons
    the C++ committee chose to disallow void main (I suspect it was just
    tradition) were not to do with the infeasibility of having void main as part
    of C++.

    john
    John Harrison, Jun 29, 2003
    #4
  5. Kelsey Bjarnason

    Rolf Magnus Guest

    John Harrison wrote:

    >>
    >> It's quite interesting that you call "void main" common practise,
    >> since it actually never was allowed. So you think the rule should be
    >> changed, because nobody follows it anyway?

    >
    > You only have to read here for a while to see that it is common
    > practise.


    Common practise for beginners, since those are mostly the ones asking
    questions together with example code that contains "void main". I guess
    that's mostly the fault of all the books that use it too. Btw, note
    that the most recommended books, i.e. the books that are considered
    best never return void from main (and no, that's not the reason why
    they are considered best).

    >> >> Fundamentally, it comes down to this: void main introduces a point
    >> >> of failure which is both completely avoidable and _trivial_ to
    >> >> avoid; why, then, risk failure?
    >> >
    >> > Are you really saying that allowing void main would put such a
    >> > strain on compilers that they could not produce correct code?

    >>
    >> Actually, that may be the case. Or a compiler may choose to not
    >> produce any code at all since the source has an error.
    >>

    >
    > That's a gem of a circular argument. void main is illegal, therefore
    > the compiler needn't compile it, therefore we can't allow void main.


    What I actually wanted to say is that void main is illegal, therefore we
    must not use it in our programs. What you seem to say is that "we" use
    void main in our programs, therefore it must be made legal.

    > What I am saying (in contrast to Kelsey) is that there is no technical
    > reason that void main could not be part of standard C++. What ever
    > reasons the C++ committee chose to disallow void main (I suspect it
    > was just tradition) were not to do with the infeasibility of having
    > void main as part of C++.


    I wonder why it's so important for you to be able to write "void main".
    What exactly doesn't work if main returns int? I don't really see a
    need to allow it.
    Btw, C does allow "void main" as an implementation defined extension in
    the C99 standard.
    Rolf Magnus, Jun 29, 2003
    #5
  6. "Rolf Magnus" <> wrote in message
    news:bdmqou$6fo$05$-online.com...
    > John Harrison wrote:
    >
    > >>
    > >> It's quite interesting that you call "void main" common practise,
    > >> since it actually never was allowed. So you think the rule should be
    > >> changed, because nobody follows it anyway?

    > >
    > > You only have to read here for a while to see that it is common
    > > practise.

    >
    > Common practise for beginners, since those are mostly the ones asking
    > questions together with example code that contains "void main". I guess
    > that's mostly the fault of all the books that use it too. Btw, note
    > that the most recommended books, i.e. the books that are considered
    > best never return void from main (and no, that's not the reason why
    > they are considered best).


    I think the book argument has some validity when the book is about C++. A
    book about C++ obviously should be written by an author who knows the
    language well. But in a book on algorithms, or graphics or networking or how
    to use the Win32 API I wouldn't hold void main against the author.

    >
    > >> >> Fundamentally, it comes down to this: void main introduces a point
    > >> >> of failure which is both completely avoidable and _trivial_ to
    > >> >> avoid; why, then, risk failure?
    > >> >
    > >> > Are you really saying that allowing void main would put such a
    > >> > strain on compilers that they could not produce correct code?
    > >>
    > >> Actually, that may be the case. Or a compiler may choose to not
    > >> produce any code at all since the source has an error.
    > >>

    > >
    > > That's a gem of a circular argument. void main is illegal, therefore
    > > the compiler needn't compile it, therefore we can't allow void main.

    >
    > What I actually wanted to say is that void main is illegal, therefore we
    > must not use it in our programs. What you seem to say is that "we" use
    > void main in our programs, therefore it must be made legal.
    >
    > > What I am saying (in contrast to Kelsey) is that there is no technical
    > > reason that void main could not be part of standard C++. What ever
    > > reasons the C++ committee chose to disallow void main (I suspect it
    > > was just tradition) were not to do with the infeasibility of having
    > > void main as part of C++.

    >
    > I wonder why it's so important for you to be able to write "void main".
    > What exactly doesn't work if main returns int? I don't really see a
    > need to allow it.
    > Btw, C does allow "void main" as an implementation defined extension in
    > the C99 standard.
    >


    You misunderstand me. I never use void main, nor do I wish to. I'm just
    saying I've yet to hear of a logical reason why it is illegal. It just seems
    to be an arbitary historical tradition. And since there doesn't seem to be
    any logic behind it, its not surprising so many people don't realise what
    the rule is.

    john
    John Harrison, Jun 29, 2003
    #6
  7. Kelsey Bjarnason

    Rolf Magnus Guest

    John Harrison wrote:

    > I think the book argument has some validity when the book is about
    > C++. A book about C++ obviously should be written by an author who
    > knows the language well. But in a book on algorithms, or graphics or
    > networking or how to use the Win32 API I wouldn't hold void main
    > against the author.


    I was more or less talking about books about C++.

    > You misunderstand me. I never use void main, nor do I wish to. I'm
    > just saying I've yet to hear of a logical reason why it is illegal. It
    > just seems to be an arbitary historical tradition. And since there
    > doesn't seem to be any logic behind it, its not surprising so many
    > people don't realise what the rule is.


    Well, C++ is meant as a portable language, and as I said, on many
    systems, every program has an exit code. What would that code be if you
    return void? Of course you could make a rule that returning void means
    returning 0 under the hood, but I'd say that this is rather a confusing
    rule. OTOH, I find it also confusing (and totally inconsistent), that
    in C++, you can leave out the return statement in main (and only in
    main) even though it has to return an int, and get an implicit return
    value of 0 in that case.
    Rolf Magnus, Jun 29, 2003
    #7
  8. Kelsey Bjarnason

    Jack Klein Guest

    On Sun, 29 Jun 2003 13:19:23 +0100, "John Harrison"
    <> wrote in comp.lang.c++:

    > >
    > > It's quite interesting that you call "void main" common practise, since
    > > it actually never was allowed. So you think the rule should be changed,
    > > because nobody follows it anyway?

    >
    > You only have to read here for a while to see that it is common practise.


    It's common practice among many of the posters here to include a
    header named <conio.h> and call a function named clrscr(). Some of
    them are quite indignant that it doesn't work with their new compiler.

    [snip]

    > What I am saying (in contrast to Kelsey) is that there is no technical
    > reason that void main could not be part of standard C++. What ever reasons
    > the C++ committee chose to disallow void main (I suspect it was just
    > tradition) were not to do with the infeasibility of having void main as part
    > of C++.
    >
    > john


    There's no reason that clrscr() is not part of standard C++. Or
    CreateWindowEXWithFunnyHungarianParameters(). They could always
    return a failure code on platforms where they physically can't exist,
    and the program could then always return EXIT_FAILURE from "void
    main()".

    Actually there's a very good reason for standard C and C++ not to
    include "void main()".

    A spoken language like English (or French, or Russian, or Hindi, or
    any other) grows and evolves through common use. In cases like this,
    things that were originally ungrammatical can, do, and should become
    part of the language if that's what a large enough percentage of the
    speakers use.

    Computer languages, on the other hand, do not, and should not have
    that flexibility. If they grow and evolve, it should be through
    careful experimentation and adoption of useful, tested and verified
    existing practice. Not just rewarding the lazy and ignorant by
    codifying their laziness and/or ignorance.

    What does C++ or C gain by standardizing "void main()"?

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Jun 29, 2003
    #8
  9. Kelsey Bjarnason

    Dill Hole Guest

    On Sun, 29 Jun 2003 13:59:15 +0200, Rolf Magnus <>
    wrote:

    >I see it a bit different. If an author uses void main, that shows that
    >he doesn't know C++ very well. This gives not much trust in the rest of
    >the book.
    >
    >
    >It's quite interesting that you call "void main" common practise, since
    >it actually never was allowed. So you think the rule should be changed,
    >because nobody follows it anyway?


    Misspelling "practice" shows that you do not know English very well.
    If you do not have English mastered why should we trust your C++
    abilities?
    Dill Hole, Jun 29, 2003
    #9
  10. Kelsey Bjarnason

    Rolf Magnus Guest

    Dill Hole wrote:

    > On Sun, 29 Jun 2003 13:59:15 +0200, Rolf Magnus <>
    > wrote:
    >
    >>I see it a bit different. If an author uses void main, that shows that
    >>he doesn't know C++ very well. This gives not much trust in the rest
    >>of the book.
    >>
    >>
    >>It's quite interesting that you call "void main" common practise,
    >>since it actually never was allowed. So you think the rule should be
    >>changed, because nobody follows it anyway?

    >
    > Misspelling "practice" shows that you do not know English very well.
    > If you do not have English mastered why should we trust your C++
    > abilities?


    Mostly because the rules of the English language are not covered by the
    C++ standard, so it has nothing to do with each other. If, OTOH, I'd
    written a book about English that teaches you wrong spelling, you
    should be suspicious, I guess.
    Rolf Magnus, Jun 29, 2003
    #10
  11. Kelsey Bjarnason

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > >It's quite interesting that you call "void main" common practise, since
    > >it actually never was allowed. So you think the rule should be changed,
    > >because nobody follows it anyway?

    >
    > Misspelling "practice" shows that you do not know English very well.


    My dictionary gives both "practice" and "practise" as proper spellings.

    > If you do not have English mastered why should we trust your C++
    > abilities?


    Mastery of English is not a necessary to mastery of C++. Given the
    ignorance of English you displayed above, you might be thankful for this
    particular fact, since it prevents your programming ability from being
    dismissed out of hand.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jun 30, 2003
    #11
  12. >
    > Actually there's a very good reason for standard C and C++ not to
    > include "void main()".
    >
    > A spoken language like English (or French, or Russian, or Hindi, or
    > any other) grows and evolves through common use. In cases like this,
    > things that were originally ungrammatical can, do, and should become
    > part of the language if that's what a large enough percentage of the
    > speakers use.
    >
    > Computer languages, on the other hand, do not, and should not have
    > that flexibility. If they grow and evolve, it should be through
    > careful experimentation and adoption of useful, tested and verified
    > existing practice. Not just rewarding the lazy and ignorant by
    > codifying their laziness and/or ignorance.


    Rewarding? That's an odd term to use, this isn't a moral debate. Adoption of
    existing practise is exactly the point I'm making.

    >
    > What does C++ or C gain by standardizing "void main()"?
    >


    Legitimising common practise, at absolutely no cost that I can see. Nothing
    else.

    I'm not suggesting that the stadnards committe should change the standard
    now, that would obviously be stupid. I'm just wondering why they didn't
    then. Or the C standards committee either.

    To return to the OP's question, 'what's the big deal?'. The big deal is that
    this trivial issue annoys a lot of people for reasons I fail to understand.

    Anyway I'm bored of this, I'd no intention of annoying so many people. Nor
    did I think my perfectly reasonable POV would be so objected to.

    john
    John Harrison, Jun 30, 2003
    #12
  13. Kelsey Bjarnason

    Rolf Magnus Guest

    Alexander Terekhov wrote:

    >
    > Jack Klein wrote:
    > [...]
    >> What does C++ or C gain by standardizing "void main()"?

    >
    > C aside for a moment, C++ would gain "a lot" by standardizing
    > "<whatever> main()".


    And what would that "lot" be?
    Rolf Magnus, Jun 30, 2003
    #13
  14. Kelsey Bjarnason

    Rolf Magnus Guest

    Jerry Coffin wrote:

    > My dictionary gives both "practice" and "practise" as proper
    > spellings.


    Mine showed that "practice" is the noun, while "practise" is (in British
    English) the verb.
    Rolf Magnus, Jun 30, 2003
    #14
  15. Rolf Magnus wrote:
    >
    > Alexander Terekhov wrote:
    >
    > >
    > > Jack Klein wrote:
    > > [...]
    > >> What does C++ or C gain by standardizing "void main()"?

    > >
    > > C aside for a moment, C++ would gain "a lot" by standardizing
    > > "<whatever> main()".

    >
    > And what would that "lot" be?


    Here's an illustration:

    http://groups.google.com/groups?threadm=
    (Subject: Re: Why int main()?)

    #include <thread>
    #include <string>
    #include <iostream>

    typedef std::joinable_thread_ptr<std::string> main_thread_ptr;

    void f(main_thread_ptr mtp) {
    main_thread_ptr::join_t result = mtp->cancel().join();
    const char * msg = std::thread_canceled(result) ?
    "canceled" : result->c_str();
    std::cout << msg << std::endl;
    }

    std::string main() {
    std::new_thread(&f, main_thread_ptr(std::thread_self()));
    std::thread_testcancel();
    std::thread_exit(std::string("hello world"));
    // Never reach here
    return "";
    }

    that's "a lot", oder?

    Well, given that void main() would also be legal [and it does make
    sense to have void main()], it would save A LOT of bandwidth and
    time on this newsgroup... to begin with. That's for sure.

    regards,
    alexander.
    Alexander Terekhov, Jun 30, 2003
    #15
  16. Dill Hole wrote:
    >
    > On Sun, 29 Jun 2003 13:59:15 +0200, Rolf Magnus <>
    > wrote:
    >
    > >I see it a bit different. If an author uses void main, that shows that
    > >he doesn't know C++ very well. This gives not much trust in the rest of
    > >the book.
    > >
    > >
    > >It's quite interesting that you call "void main" common practise, since
    > >it actually never was allowed. So you think the rule should be changed,
    > >because nobody follows it anyway?

    >
    > Misspelling "practice" shows that you do not know English very well.
    > If you do not have English mastered why should we trust your C++
    > abilities?


    Come back if you can read, write and speek german in a way
    comparable to Rolf's english skills.
    Until then, go away.

    Thank you.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jun 30, 2003
    #16
  17. Kelsey Bjarnason

    Jerry Coffin Guest

    In article <bdp4f7$n5g$03$-online.com>,
    says...
    > Jerry Coffin wrote:
    >
    > > My dictionary gives both "practice" and "practise" as proper
    > > spellings.

    >
    > Mine showed that "practice" is the noun, while "practise" is (in British
    > English) the verb.


    Admittedly, I'm using a rather larger dictionary than most (not to
    mention a rather elderly one) but it lists practice as both a noun and a
    verb. It lists practise as a verb, but the last time I noticed, verbals
    (e.g. gerunds) were still considered grammatical, so using a word that's
    listed as a verb in a noun-like situation isn't necessarily a
    grammatical error. Offhand, I no longer remember whether the original
    use looked like a gerund or not, but the dictionary does list a
    participle form for practise, which would at least allow the
    possibility.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jun 30, 2003
    #17
  18. Kelsey Bjarnason

    Andre Kostur Guest

    Alexander Terekhov <> wrote in news:3F001F83.96AF652
    @web.de:

    >
    > Rolf Magnus wrote:
    >>
    >> Alexander Terekhov wrote:
    >>
    >> >
    >> > Jack Klein wrote:
    >> > [...]
    >> >> What does C++ or C gain by standardizing "void main()"?
    >> >
    >> > C aside for a moment, C++ would gain "a lot" by standardizing
    >> > "<whatever> main()".

    >>
    >> And what would that "lot" be?

    >
    > Here's an illustration:
    >
    > http://groups.google.com/groups?threadm=
    > (Subject: Re: Why int main()?)
    >
    > #include <thread>
    > #include <string>
    > #include <iostream>
    >
    > typedef std::joinable_thread_ptr<std::string> main_thread_ptr;
    >
    > void f(main_thread_ptr mtp) {
    > main_thread_ptr::join_t result = mtp->cancel().join();
    > const char * msg = std::thread_canceled(result) ?
    > "canceled" : result->c_str();
    > std::cout << msg << std::endl;
    > }
    >
    > std::string main() {
    > std::new_thread(&f, main_thread_ptr(std::thread_self()));
    > std::thread_testcancel();
    > std::thread_exit(std::string("hello world"));
    > // Never reach here
    > return "";
    > }
    >
    > that's "a lot", oder?


    That places a lot of burden on every OS that this application would need
    to run on. Since your application needs to return a std::string to the
    OS, which implementation of std::string should the OS expect? STLport?
    Dinkumware? The Kostur STL? How about this main() :

    MyOwnWeirdClass main()
    {
    return MyOwnWeirdClass();
    }

    What's the OS going to do with _that_ return value? It has no hope of
    knowing what the heck is in that class, so there's not a whole lot that
    it can do with it (intelligently...).

    (Keep in mind that threading isn't defined in Standard C++, thus is not a
    good basis for argument in this forum)

    > Well, given that void main() would also be legal [and it does make
    > sense to have void main()], it would save A LOT of bandwidth and
    > time on this newsgroup... to begin with. That's for sure.
    >
    > regards,
    > alexander.
    >
    Andre Kostur, Jun 30, 2003
    #18
  19. Kelsey Bjarnason

    Default User Guest

    John Harrison wrote:

    > That's a gem of a circular argument. void main is illegal, therefore the
    > compiler needn't compile it, therefore we can't allow void main.



    That's not circular at all, it's linear. void main is illegal IN THE
    STANDARD, so a compiler needn't compile it, so we (comp.lang.c++) can't
    allow void main. Where is the circularity? There is none.



    Brian Rodenborn
    Default User, Jun 30, 2003
    #19
  20. Andre Kostur wrote:
    [...]
    > > std::string main() {
    > > std::new_thread(&f, main_thread_ptr(std::thread_self()));
    > > std::thread_testcancel();
    > > std::thread_exit(std::string("hello world"));
    > > // Never reach here
    > > return "";
    > > }
    > >
    > > that's "a lot", oder?

    >
    > That places a lot of burden on every OS that this application would need
    > to run on. Since your application needs to return a std::string to the
    > OS, ....


    It isn't meant to return "a std::string to the OS". Try again.
    Slowly, this time. That program uses a "passive exit"... that
    returns int(0) "to the OS".

    regards,
    alexander.
    Alexander Terekhov, Jun 30, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian
    Replies:
    0
    Views:
    648
  2. John Harrison

    Re: Big Deal with returning int

    John Harrison, Jun 29, 2003, in forum: C++
    Replies:
    5
    Views:
    820
    Karl Heinz Buchegger
    Jun 30, 2003
  3. Rolf Magnus

    Re: Big Deal with returning int

    Rolf Magnus, Jun 29, 2003, in forum: C++
    Replies:
    0
    Views:
    629
    Rolf Magnus
    Jun 29, 2003
  4. Samuele Armondi

    Re: Big Deal with returning int

    Samuele Armondi, Jun 29, 2003, in forum: C++
    Replies:
    0
    Views:
    690
    Samuele Armondi
    Jun 29, 2003
  5. Jonathan Clements

    Re: Big Deal with returning int

    Jonathan Clements, Jun 29, 2003, in forum: C++
    Replies:
    7
    Views:
    1,660
    Rolf Magnus
    Jul 1, 2003
Loading...

Share This Page