Topic: Can main be a template?
Author: David R Tribble <dtribble@technologist.com>
Date: 1998/12/19 Raw View
AllanW@my-dejanews.com wrote:
> ...
> In any case, the standard lists two specific definitions of
> main [3.6.1]:
> int main() { /* ... */ }
> and
> int main(int argc, char *argv[]) { /* ... */ }
> A conforming implementation doesn't have to accept ANY other
> definitions, including the templates listed above.
>
> BTW, does that mean that
> int main(int argCount, char *argValues[]) { /* ... */ }
> doesn't have to be accepted (wrong parameter names)? Or
> int main(int argc, char**argv) { /* ... */ }
> where parameter's declaration is equivalent but not identical? Or
> int main(int argc, char *argv[])
> { std::cout << "Hello world!" << std::endl; }
> where the function body doesn't match what's in the spec?
>
> Don't quote common sense; I know as well as you do that all
> three of these ought to work. But look at the spec. Doesn't
> it say that these definitions of main don't have to be
> accepted by a conforming compiler?
The standard states elsewhere (chap 13) that the parameter names
of a function are not part of its signature (and neither are
certain appearances of 'const' specifiers on the parameters).
Obviously, then, only the signature is relevant to proper
name-matching and invocation of a given function. It is not
stated that main() does not follow these rules, so we can infer
than it follows the same rules as any other function. If you
can prove the standard states or implies otherwise, though, you
may have a case.
Remember that just because one clause in the standard fails to
mention some relevant rule, this does not invalidate those
clauses in the standard that are pertinent. In other words, it
can be misleading to quote a clause without quoting all of the
other relevant clauses as well.
-- 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/12/16 Raw View
scott douglass wrote:
>
> Steve Clamage (stephen.clamage@sun.com) wrote:
> [...]
> : It says in
> : 3.6.1, paragraph 2:
>
> : ========
> : An implementation shall not predefine the main function. This function
> : shall not be overloaded. It shall have a return type of type int,
> : but otherwise its type is implementation-defined.
> : ========
>
> Does this mean that the following is a legal program:
> template <class T, class S> T main(T, S) { }
> template int main(int, char**);
> ?
An interesting question.
A template by itself does not generate a function. Only instantiation
of a template generates a function. As long as there's only one
instantiation of the template, there's no overloading of main either.
Since the specialisation here conforms to the requirements, I'd say
it should be legal.
The other question is if you find any implementation that supports
this definition. I doubt so. Certainly no implementation is required
to accept it.
>
> What about just this:
> template <class T, class S> T main(T, S) { }
> ?
This IMHO doesn't define a main function. So this would
not be legal C++ (unless it's a freestanding implementation
where AFAIK main is not required).
[ 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: Hyman Rosen <hymie@prolifics.com>
Date: 1998/12/16 Raw View
Section 3.6.1 says "A program shall contain a global function called main".
Defining a function template called main certainly doesn't satisfy that.
The functions instantiated from a function template don't have names,
exactly,
but template-ids, as per section 14.2. So I would say that you cannot use a
function template to form main.
[ 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@my-dejanews.com
Date: 1998/12/18 Raw View
In article <36777651.EB2C5C9C@physik.tu-muenchen.de>,
Christopher Eltschka <celtschk@physik.tu-muenchen.de> wrote:
>
> scott douglass wrote:
> >
> > Steve Clamage (stephen.clamage@sun.com) wrote:
> > [...]
> > : It says in
> > : 3.6.1, paragraph 2:
> >
> > : ========
> > : An implementation shall not predefine the main function. This function
> > : shall not be overloaded. It shall have a return type of type int,
> > : but otherwise its type is implementation-defined.
> > : ========
> >
> > Does this mean that the following is a legal program:
> > template <class T, class S> T main(T, S) { }
> > template int main(int, char**);
> > ?
>
> An interesting question.
> A template by itself does not generate a function. Only instantiation
> of a template generates a function. As long as there's only one
> instantiation of the template, there's no overloading of main either.
> Since the specialisation here conforms to the requirements, I'd say
> it should be legal.
> The other question is if you find any implementation that supports
> this definition. I doubt so. Certainly no implementation is required
> to accept it.
I'm not sure what it means to say that something is legal, but
no implementation is required to accept it.
In any case, the standard lists two specific definitions of
main:
int main() { /* ... */ }
and
int main(int argc, char *argv[]) { /* ... */ }
A conforming implementation doesn't have to accept ANY other
definitions, including the templates listed above.
BTW, does that mean that
int main(int argCount, char *argValues[]) { /* ... */ }
doesn't have to be accepted (wrong parameter names)? Or
int main(int argc, char**argv) { /* ... */ }
where parameter's declaration is equivalent but not identical? Or
int main(int argc, char *argv[])
{ std::cout << "Hello world!" << std::endl; }
where the function body doesn't match what's in the spec?
Don't quote common sense; I know as well as you do that all
three of these ought to work. But look at the spec. Doesn't
it say that these definitions of main don't have to be
accepted by a conforming compiler?
--
AllanW@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: scott douglass <sdouglass%_%junk@_.arm.com>
Date: 1998/12/15 Raw View
Steve Clamage (stephen.clamage@sun.com) wrote:
[...]
: It says in
: 3.6.1, paragraph 2:
: ========
: An implementation shall not predefine the main function. This function
: shall not be overloaded. It shall have a return type of type int,
: but otherwise its type is implementation-defined.
: ========
Does this mean that the following is a legal program:
template <class T, class S> T main(T, S) { }
template int main(int, char**);
?
What about just this:
template <class T, class S> T main(T, S) { }
?
[ 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: stanley@West.sun.com (Stanley Friesen [Contractor])
Date: 1998/12/15 Raw View
In article <755og2$7qi@sis.cambridge.arm.com>,
scott douglass <sdouglass%_%junk@_.arm.com> wrote:
>
>Steve Clamage (stephen.clamage@sun.com) wrote:
>[...]
>: It says in
>: 3.6.1, paragraph 2:
>
>: ========
>: An implementation shall not predefine the main function. This function
>: shall not be overloaded. It shall have a return type of type int,
>: but otherwise its type is implementation-defined.
>: ========
>
>Does this mean that the following is a legal program:
> template <class T, class S> T main(T, S) { }
> template int main(int, char**);
>?
I think this fails the "shall not be overloaded" part, as template
functions are always implicitly overloaded. At least that would be
my call, but I am not the committee.
[ 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 ]