 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Thomas Tutone Guest
|
Posted: Wed Jul 06, 2005 10:04 pm Post subject: Wrapping an overloaded main in a namespace |
|
|
I have been experimenting with SmartWin
([url]http://smartwin.sourceforge.net)[/url], which is an
interesting C++ framework for windows GUI programming
that takes what its creators call a "modern C++
approach." This means, among other things, that it
makes very heavy use of templates and a number of
techniques drawn from Modern C++ Design.
Unfortunately, it also uses certain other techniques
that appear not to conform to the Standard. SmartWin
was written using Visual C++ 7.1, and my attempts to
make it work in GCC have made some of these
questionable techniques apparent.
One of these techniques is an overloaded main.
Stripped of the OS-specific details, the technique
amounts to the following:
class X {};
int main(X&);
int main()
{
X x;
main(x);
}
The idea is that the user of SmartWin defines
int main(X&), and SmartWin's internals call the real
main behind the scenes (and in the process deal with
various windows initialization code that is insulated
from the user).
It seems to me that the above code is illegal, or at
least results in undefined behavior, by overloading
main at global scope. Am I correct?
Rather than merely criticize, though, I would like to
suggest a fix to the problem that would require only
minor modifications to existing SmartWin code. Does
wrapping the overloaded main in a namespace help? In
other words, consider the following:
class X {};
namespace N {
int main(X&);
}
using N::main;
int main()
{
X x;
main(x);
}
Is this now legal C++?
Thanks.
Best regards,
Tom
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gene Bushuyev Guest
|
Posted: Thu Jul 07, 2005 12:31 pm Post subject: Re: Wrapping an overloaded main in a namespace |
|
|
"Thomas Tutone" <thomas8675309 (AT) yahoo (DOT) com> wrote
| Quote: | I have been experimenting with SmartWin
....
Rather than merely criticize, though, I would like to
suggest a fix to the problem that would require only
minor modifications to existing SmartWin code. Does
wrapping the overloaded main in a namespace help? In
other words, consider the following:
class X {};
namespace N {
int main(X&);
}
using N::main;
int main()
{
X x;
main(x);
}
Is this now legal C++?
|
Ye, it is legal. The requirements standard imposes are related only to main
in the global namespace. You can have a function called main in another
namespace (3.6.1/3):
"3 The function main shall not be used (3.2) within a program. The linkage
(3.5) of main is
implementation-defined. A program that declares main to be inline or static
is ill-formed. The
name main is not otherwise reserved. [Example: member functions, classes,
and enumerations can be
called main, as can entities in other namespaces."
Gene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mathias Waack Guest
|
Posted: Thu Jul 07, 2005 12:32 pm Post subject: Re: Wrapping an overloaded main in a namespace |
|
|
Thomas Tutone wrote:
| Quote: | One of these techniques is an overloaded main.
Stripped of the OS-specific details, the technique
amounts to the following:
class X {};
int main(X&);
int main()
{
X x;
main(x);
}
|
It is not legal: 3.6.1.2: This [the main] function shall not be overloaded.
| Quote: | Rather than merely criticize, though, I would like to
suggest a fix to the problem that would require only
minor modifications to existing SmartWin code. Does
wrapping the overloaded main in a namespace help? In
other words, consider the following:
class X {};
namespace N {
int main(X&);
}
using N::main;
int main()
{
X x;
main(x);
}
Is this now legal C++?
|
Sure. N::main is not special, it can be overloaded as usual. The standard
states this explicitly in 3.6.1.3.
Mathias
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gianluca Silvestri Guest
|
Posted: Thu Jul 07, 2005 12:32 pm Post subject: Re: Wrapping an overloaded main in a namespace |
|
|
"Thomas Tutone" <thomas8675309 (AT) yahoo (DOT) com> ha scritto nel messaggio
news:20050706143626.47016.qmail (AT) web60915 (DOT) mail.yahoo.com...
| Quote: | I have been experimenting with SmartWin
([url]http://smartwin.sourceforge.net)[/url], which is an
interesting C++ framework for windows GUI programming
that takes what its creators call a "modern C++
approach." This means, among other things, that it
makes very heavy use of templates and a number of
techniques drawn from Modern C++ Design.
Unfortunately, it also uses certain other techniques
that appear not to conform to the Standard. SmartWin
was written using Visual C++ 7.1, and my attempts to
make it work in GCC have made some of these
questionable techniques apparent.
One of these techniques is an overloaded main.
Stripped of the OS-specific details, the technique
amounts to the following:
class X {};
int main(X&);
int main()
{
X x;
main(x);
}
The idea is that the user of SmartWin defines
int main(X&), and SmartWin's internals call the real
main behind the scenes (and in the process deal with
various windows initialization code that is insulated
from the user).
It seems to me that the above code is illegal, or at
least results in undefined behavior, by overloading
main at global scope. Am I correct?
Yes. The Standard says that main() cannot be overloaded. |
| Quote: |
Rather than merely criticize, though, I would like to
suggest a fix to the problem that would require only
minor modifications to existing SmartWin code. Does
wrapping the overloaded main in a namespace help? In
other words, consider the following:
class X {};
namespace N {
int main(X&);
}
using N::main;
The using brings N::main(X&) into the same scope of main(). That means |
overloading, which is forbidden.
| Quote: |
int main()
{
X x;
main(x);
}
Is this now legal C++?
Why not simply call the main(X&) something like: my_main(X&) and let it be |
called from main():
int main()
{
X x;
my_main(X);
}
Or, if you want, I think it would be conforming to remove the
using-declaration and qualify the function call:
int main()
{
X x;
N::main(x);
}
HTH
Gianluca
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tokyo Tomy Guest
|
Posted: Thu Jul 07, 2005 12:33 pm Post subject: Re: Wrapping an overloaded main in a namespace |
|
|
Thomas Tutone <thomas8675309 (AT) yahoo (DOT) com> wrote
[snip]
| Quote: | One of these techniques is an overloaded main.
Stripped of the OS-specific details, the technique
amounts to the following:
class X {};
int main(X&);
int main()
{
X x;
main(x);
}
The idea is that the user of SmartWin defines
int main(X&), and SmartWin's internals call the real
main behind the scenes (and in the process deal with
various windows initialization code that is insulated
from the user).
|
What do you mean by "the real main"?
| Quote: | It seems to me that the above code is illegal, or at
least results in undefined behavior, by overloading
main at global scope. Am I correct?
|
I think the above code is illegal. The Standard says this function
(i.e. main) shall not be overloaded at 3.6.1/2.
| Quote: | Rather than merely criticize, though, I would like to
suggest a fix to the problem that would require only
minor modifications to existing SmartWin code. Does
wrapping the overloaded main in a namespace help? In
other words, consider the following:
class X {};
namespace N {
int main(X&);
}
using N::main;
int main()
{
X x;
main(x);
}
Is this now legal C++?
|
No, it is not legal, because the Standard say main is global at
3.6.1/1 and shall not be used within a program at 3.6.1/3.
I think N::main is an ordinary function, which has no ability to start
another process, receiving parameters from the environment outside the
process.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Sat Jul 09, 2005 3:47 am Post subject: Re: Wrapping an overloaded main in a namespace |
|
|
Gianluca Silvestri wrote:
| Quote: | "Thomas Tutone" <thomas8675309 (AT) yahoo (DOT) com> ha scritto nel messaggio
news:20050706143626.47016.qmail (AT) web60915 (DOT) mail.yahoo.com...
I have been experimenting with SmartWin
([url]http://smartwin.sourceforge.net)[/url], which is an
interesting C++ framework for windows GUI programming
that takes what its creators call a "modern C++
approach." This means, among other things, that it
makes very heavy use of templates and a number of
techniques drawn from Modern C++ Design.
Unfortunately, it also uses certain other techniques
that appear not to conform to the Standard. SmartWin
was written using Visual C++ 7.1, and my attempts to
make it work in GCC have made some of these
questionable techniques apparent.
One of these techniques is an overloaded main.
Stripped of the OS-specific details, the technique
amounts to the following:
class X {};
int main(X&);
int main()
{
X x;
main(x);
}
The idea is that the user of SmartWin defines
int main(X&), and SmartWin's internals call the real
main behind the scenes (and in the process deal with
various windows initialization code that is insulated
from the user).
It seems to me that the above code is illegal, or at
least results in undefined behavior, by overloading
main at global scope. Am I correct?
Yes. The Standard says that main() cannot be overloaded.
Rather than merely criticize, though, I would like to
suggest a fix to the problem that would require only
minor modifications to existing SmartWin code. Does
wrapping the overloaded main in a namespace help? In
other words, consider the following:
class X {};
namespace N {
int main(X&);
}
using N::main;
The using brings N::main(X&) into the same scope of main(). That means
overloading, which is forbidden.
|
Function overloading occurs only with functions of the same name at the
same scope. Since N::main is declared within its own namespace and not
at the global scope, it does not override global main() - the
subsequent using declaration notwithstanding.
Although of questionable usefulness, the code as proposed by the
original poster, is legal.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|