Topic: Trick Question...


Author: James.Kanze@dresdner-bank.com
Date: 1999/04/23
Raw View
In article <7fld2h$pb0$1@nnrp1.dejanews.com>,
  "AllanW {formerly AllanW@my-dejanews.com}" <allan_w@my-dejanews.com> wrote:

> The C run-time system has to intialize all of it's library
> members and all user global variables before it passes control
> to main(), and it must perform any global destruction and
> library clean-up after main() returns. How is this implemented?
> One obvious method is to have the library contain the "real"
> entry point for all programs, and have it call main(). This
> is, in fact, how every library I'm familiar with works.

I beleive that CFront recognized the function main, and inserted the
necessary initialization code in it.  Any compiler that tries to use the
same start-off routine (traditionally, ctr0 under UNIX) will do the
same.

Another reason why the compiler must treat main special is more general:
all compilers I know mangle function names for type-safe linkage.  Of
course, if the start-up routine is common with C, it is going to call a
function with the unmangled name.  But even if it isn't, the mangling,
if any, has to be special, because whether I call it main() or main(
int, char** ), the same function must be called.  (This is also the
reason why main can't be declared other than in its definition.)

--
James Kanze                         mailto: James.Kanze@dresdner-bank.com
Conseils en informatique orient   e objet/
                        Beratung in objekt orientierter Datenverarbeitung
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany  Tel. +49 (069) 63 19 86 27

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "AllanW {formerly AllanW@my-dejanews.com}" <allan_w@my-dejanews.com>
Date: 1999/04/22
Raw View
In article <371CEAA5.8B183E98@technologist.com>,
  David R Tribble <dtribble@technologist.com> wrote:
> I don't see the conflict; static names in one source file don't
> conflict with static or extern names in another source file; so
> why can't I have a static main() in one source file, and the
> global non-static main() in another file?

The C run-time system has to intialize all of it's library
members and all user global variables before it passes control
to main(), and it must perform any global destruction and
library clean-up after main() returns. How is this implemented?
One obvious method is to have the library contain the "real"
entry point for all programs, and have it call main(). This
is, in fact, how every library I'm familiar with works.

But another equally valid method is to have the compiler
recognize name "main" as special case. Here the compiler would
insert a call to the library initialization before the first
executable source code statement in main, and would also
treat all "return" statements as equivalent calls to exit().
BTW this is why main() is not allowed to be re-entrant.

I suppose it's still possible for the compiler to distinguish
between a static file-scope main() and an extern main(), but
the distinction is getting more and more blurry.

----
Allan_W@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <dtribble@technologist.com>
Date: 1999/04/20
Raw View
David R Tribble <dtribble@technologist.com> writes:
>>    namespace /*unnamed*/
>>    {
>>        int main()
>>        {
>>            ...
>>        }
>>
>>        void someFunc()
>>        {
>>            int i = main();
>>        }
>>    }
>>
>> I think that this is legal (it is not mentioned in 3.6.1 or
>> 7.3.1.1, and 3.6.1#3 seems to imply it is legal).
>>
>> If so, then we have identified a subtle difference between 'static'
>> and 'namespace /*unnamed*/'.

Steve Clamage wrote:
> Yes. The "main" is in a namespace other than the global namespace,
> so it is OK.
>
> I don't know how subtle the differences are; they are just
> different. Declaring a function or object "static" gives it
> internal linkage. Any namespace-scope name not declared static
> has external linkage.
>
> The oddity with the unnamed namespace is that it acts like a
> namespace whose name cannot be written.  Even though its non-
> static members have external linkage, the members of an unnamed
> namespace cannot be accessed by name from other translation units
> because the namespace name has no user-accessible spelling.

All of this makes sense.

> You can't have a static main in the global namespace because
> it could conflict with the required non-static main in the
> global namespace. Putting an entity called main in any other
> namespace cannot cause that conflict.

I don't see the conflict; static names in one source file don't
conflict with static or extern names in another source file; so
why can't I have a static main() in one source file, and the
global non-static main() in another file?

    //-------------------------------------------
    // one.cpp

    int  foo = 1;

    extern void  func();

    int main(int argc, char **argv)
    {
        ...
        func();
        ...
    }

    //-------------------------------------------
    // two.cpp

    static int  foo = 2;

    static int main()
    {
        ...
    }

    void func()
    {
        main();
    }

-- David R. Tribble, dtribble@technologist.com --


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/04/20
Raw View
David R Tribble <dtribble@technologist.com> writes:

>    namespace /*unnamed*/
>    {
>        int main()
>        {
>            ...
>        }

>        void someFunc()
>        {
>            int i = main();
>        }
>    }

>I think that this is legal (it is not mentioned in 3.6.1 or
>7.3.1.1, and 3.6.1#3 seems to imply it is legal).

Yes. The "main" is in a namespace other than the global namespace,
so it is OK.

>If so, then we
>have identified a subtle difference between 'static' and
>'namespace /*unnamed*/'.

I don't know how subtle the differences are; they are just
different. Declaring a function or object "static" gives it
internal linkage. Any namespace-scope name not declared static
has external linkage.

The oddity with the unnamed namespace is that it acts like a
namespace whose name cannot be written.  Even though its non-
static members have external linkage, the members of an unnamed
namespace cannot be accessed by name from other translation units
because the naemspace name has no user-accessible spelling.

You can't have a static main in the global namespace because
it could conflict with the required non-static main in the
global namespace. Putting an entity called main in any other
namespace cannot cause that conflict.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <dtribble@technologist.com>
Date: 1999/04/16
Raw View
David R Tribble <dtribble@technologist.com> writes:
>> My question: Is the following legal?
>>
>>    static void main()
>>    {
>>        ...
>>    }
>>
>>    void someFunc()
>>    {
>>        main();
>>    }

Steve Clamage wrote:
> No. See 3.6.1, which says you can't have a static main.

David:
>> Or how about:
>>
>>    namespace MyOwn
>>    {
>>        int main()
>>        {
>>            ...
>>        }
>>
>>        void someFunc()
>>        {
>>            int i = main();
>>        }
>>    }

Steve:
> Yes. See the same section.

Okay, then how about:

    namespace /*unnamed*/
    {
        int main()
        {
            ...
        }

        void someFunc()
        {
            int i = main();
        }
    }

I think that this is legal (it is not mentioned in 3.6.1 or
7.3.1.1, and 3.6.1#3 seems to imply it is legal).  If so, then we
have identified a subtle difference between 'static' and
'namespace /*unnamed*/'.

-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: paul@ra.avid.com (Paul Miller)
Date: 1999/04/13
Raw View
paulp@ccnet.com (Paul Pedriana) writes:

>Is the following legal code? If so, then why/how?

Yes, if you put this in front:

class SomeClass
{
 void main();
 void SomeFunction():
};

>   void SomeClass::SomeFunction(){
>      main();
>   }

>Clue: This is a trick question.

--
Paul T. Miller                | Paul_Miller@avid.com
Principal Engineer            | Opinions expressed here are my own.
Avid Technology, Inc. - Graphics and Effects Software Group
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: paulp.removethis@ccnet.com (Paul)
Date: 1999/04/13
Raw View
The idea is that it can be legal code if
SomeClass is defined in such a way to make it
so. Somebody I knew saw some code like that
recently and said "You can't call main from there."
The trick, of course, is that you can call main
if it is simply some 'main' other than global
main, which (unlike C) can't be called from
code. (std. 3.6.1)
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/04/15
Raw View
David R Tribble <dtribble@technologist.com> writes:

>My question: Is the following legal?

>    static void main()
>    {
>        ...
>    }

>    void someFunc()
>    {
>        main();
>    }

No. See 3.6.1, which says you can't have a static main.

>Or how about:

>    namespace MyOwn
>    {
>        int main()
>        {
>            ...
>        }

>        void someFunc()
>        {
>            int i = main();
>        }
>    }

Yes. See the same section.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <dtribble@technologist.com>
Date: 1999/04/14
Raw View
Paul Pedriana wrote:
>
> Is the following legal code?  If so, then why/how?
>
>    void SomeClass::SomeFunction() {
>       main();
>    }
>
> Clue: This is a trick question.

Answer:
  No, if 'main' refers to '::main()'.

  Yes, if 'main' refers to any other function, such as a member
  function of SomeClass or one of its base classes.

I do this all the time, because I don't like free (non-member)
functions, but C++ requires ::main() to be one:

    int main(int argc, char **argv)
    {
        Program   pgm;

        return pgm.main(argc, (const char *const *) argv);
    }

My question: Is the following legal?

    static void main()
    {
        ...
    }

    void someFunc()
    {
        main();
    }

Or how about:

    namespace MyOwn
    {
        int main()
        {
            ...
        }

        void someFunc()
        {
            int i = main();
        }
    }

-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: paulp@ccnet.com (Paul Pedriana)
Date: 1999/04/11
Raw View
Is the following legal code? If so, then why/how?

   void SomeClass::SomeFunction(){
      main();
   }

Clue: This is a trick question.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/04/11
Raw View
paulp@ccnet.com (Paul Pedriana) writes:

>Is the following legal code? If so, then why/how?

>   void SomeClass::SomeFunction(){
>      main();
>   }

>Clue: This is a trick question.

Yes, a trick question. You haven't provided enough information to
answer the question.

The identifier "main" is reserved in global scope for use as a
function name. If a non-global function named "main" is visible
and accessible from the scope of SomeClass, the code is OK.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/04/12
Raw View
Paul Pedriana wrote:
>
> Is the following legal code? If so, then why/how?
>
>    void SomeClass::SomeFunction(){
>       main();
>    }
>
> Clue: This is a trick question.

Not legal - there's no declaration of SomeClass in scope. Did you leave
something out, or was that the trick?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]