This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Is is possible to use the name of a variable in the constructor?
- From: Ian Lance Taylor <ian at airs dot com>
- To: Gunther Piez <gpiez at web dot de>
- Cc: gcc-help at gcc dot gnu dot org
- Date: 31 Jul 2005 17:04:03 -0700
- Subject: Re: Is is possible to use the name of a variable in the constructor?
- References: <200508010155.53152.gpiez@web.de>
Gunther Piez <gpiez@web.de> writes:
> I'm looking for something like __PRETTY_FUNCTION__ for a variable (or a
> object).
> It want to do
>
> struct uniform {
> uniform() {
> cout << "A variable called " << __PRETTY_VARNAME__ << " was just
> instantinated (sp)" << endl;
> }
> };
>
> void main() {
> uniform blah;
> }
>
>
> and get "A variable called blah was just instantinated" as program output.
> This must work at runtime. Is there a identifier or macro which holds the
> desired value?
No.
Remember that in general uniform::uniform may be in different source
file, and may be compiled before the function which uses a variable of
type uniform. The only way this could be made to work would be to
have the compiler secretly pass the variable name into the function.
There is no infrastructure for noting which functions would require
the variable name, or for passing in the variable name.
It would be possible to make this work in very specific cases, but I
don't think this type of language extension would be very interesting
if it couldn't work in the general case.
Ian