Thread: Why cant I overload main - when its already overloaded?!

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    United States
    Posts
    22

    Why cant I overload main - when its already overloaded?!

    If there are two versions of main() why cant I overload them in my code, when they are already overloaded as two of the same function for C++?

    test1.cpp
    Code:
    #include <iostream>
    
    using namespace std;
    
    //function overloading of main();
    int main(void);
    int main(int argc, char *argv[]);
    
    int main(void)
    {
       cout << "hello world" << endl;
       return 0;
    }
    
    int main(int argc, char *argv[])
    {
        int x;
        for(x = 0;x < argc;x++) { cout << argv[x] << endl; }
        return 0; 
    }
    gcc 4.8.1 wont let me compile this even though its a valid c++ function like any other function from what I have read in my c++ textbook . The book doesn't mention main() as an exception, so I thought that if there are two versions of main() that are valid, function overloading of main() could be done.

    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yes, main is an exception. The program has exactly one entry point, which is the one main function. Calling main from code is also prohibited.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    May 2013
    Location
    United States
    Posts
    22
    Quote Originally Posted by King Mir View Post
    Yes, main is an exception. The program has exactly one entry point, which is the one main function. Calling main from code is also prohibited.
    Isn't there a feature in gcc where you can use main() as a overloaded function as in my code above?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not in the way you mean, no.

    It is possible to provide overloaded functions named main() in a different namespace. But that doesn't work for the program entry point named main().

    Only one version of main() - as in the entry point - is permitted in a program. The only choice you have is which form it has.
    Last edited by grumpy; 07-12-2013 at 05:48 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can just create another function with a different name and delegate to it. Eg:

    Code:
    void foo();
    void foo(int argc, char** argv);
    
    int main(int argc, char** argv) { /* Call appropriate foo */ }
    Not that I see the need to overload main. Why would you need it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Just out of curiosity, for the OP, what do you expect to happen as a result of compiling/linking/executing your test1.cpp?

    Do you expect one version of main() to be called, then the other? Do you expect them to be executed in parallel on different threads? Do you expect their output to be interleaved? Or what?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    The main() function implicitly has C linkage - that is, the compiler treats this:

    Code:
    int main()
    As if you'd "really" written this:

    Code:
    extern "C" int main()
    Function overloading requires the use of mangled names (so the linker can tell which version of the symbol is being used), and extern "C" turns off this name mangling (because C doesn't have it). Thus, you cannot overload main(). This also means, as others have pointed out, that your program will always have exactly one entry point - i.e. it makes no sense to overload main().
    Last edited by JohnGraham; 07-14-2013 at 12:12 PM.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by King Mir View Post
    Calling main from code is also prohibited.
    I don't know where you got that from - main() is just an ordinary function, and can be called from within a program just like any other. Granted it's not something I'd consider "normal" to do but it's certainly not violating any of the rules of C or C++.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by JohnGraham View Post
    The main() function implicitly has C linkage - that is, the compiler treats this:
    The standard disagrees with you. It says:
    Quote Originally Posted by C++14 Standard Draft
    This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.
    and
    Quote Originally Posted by C++14 Standard Draft
    The linkage (3.5) of main is implementation-defined.
    Quote Originally Posted by JohnGraham View Post
    I don't know where you got that from - main() is just an ordinary function, and can be called from within a program just like any other. Granted it's not something I'd consider "normal" to do but it's certainly not violating any of the rules of C or C++.
    Again, the standard disagrees with you:
    Quote Originally Posted by C++14 Standard Draft
    The function main shall not be used within a program
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by JohnGraham View Post
    I don't know where you got that from - main() is just an ordinary function, and can be called from within a program just like any other. Granted it's not something I'd consider "normal" to do but it's certainly not violating any of the rules of C or C++.
    As Elysia quoted, it's explicitly not allowed by the standard, and it has been disallowed since the beginning. The reason for this rule, is to allow compilers to put static initialization and program boilerplate code into the body of main as an optimization. Many modern compilers don't do this optimization and will therefore allow you to call main directly, as a compiler extension.
    Last edited by King Mir; 07-14-2013 at 01:04 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by grumpy View Post
    Just out of curiosity, for the OP, what do you expect to happen as a result of compiling/linking/executing your test1.cpp?
    My guess is that the OP expects int main(int argc, char *argv[]) to be called if any command line parameters are present, if not then int main(void) is called. The usefulness of this is questionable of course, since it is just a matter of checking whether argc > 1.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Fair enough - I stand corrected!
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The various signatures of main() are not overloads. main() is a C function, not a C++ function. Its prototype looks like this:

    Code:
    int main();
    Which in C-land, means main() is a function taking zero or more parameters. So the various incarnations of main() are actually one incarnation, not overloads of each other.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by brewbuck View Post
    The various signatures of main() are not overloads. main() is a C function, not a C++ function. Its prototype looks like this:

    Code:
    int main();
    Which in C-land, means main() is a function taking zero or more parameters. So the various incarnations of main() are actually one incarnation, not overloads of each other.
    A number of things are incorrect here.

    Firstly, main is a C++ function by virtue of being described explicitly by the C++ standard. It may have C linkage on some compilers, but the C++ standard redefines the behavior of main, and does not mandate any C compatibility, except by describing identical semantics (except for implicit return 0 prior to C99).

    Second, while a C prototype written like that would describe a function dating zero or more parameters, a function definition starting that way does not. A function definition using () strictly means zero parameters.

    Thirdly, one can validly write a prototype for main in C++ or C as follows, explicitly stating that main has zero parameters:
    Code:
    int main(void);

    It is true that the various ways main is allowed to be defined are not overloads, however.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  2. Circular main <- main.o dependency dropped.
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2005, 02:32 PM
  3. A question about void(main) and int(main)
    By edd1986 in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 03:18 PM
  4. Replies: 5
    Last Post: 11-24-2002, 11:05 PM
  5. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM

Tags for this Thread