public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Is is possible to use the name of a variable in the constructor?
@ 2005-07-31 23:56 Gunther Piez
  2005-08-01  0:04 ` Ian Lance Taylor
  2005-08-01 11:29 ` Eljay Love-Jensen
  0 siblings, 2 replies; 7+ messages in thread
From: Gunther Piez @ 2005-07-31 23:56 UTC (permalink / raw)
  To: gcc-help

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?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is is possible to use the name of a variable in the constructor?
  2005-07-31 23:56 Is is possible to use the name of a variable in the constructor? Gunther Piez
@ 2005-08-01  0:04 ` Ian Lance Taylor
  2005-08-01  0:32   ` Gunther Piez
  2005-08-01 11:29 ` Eljay Love-Jensen
  1 sibling, 1 reply; 7+ messages in thread
From: Ian Lance Taylor @ 2005-08-01  0:04 UTC (permalink / raw)
  To: Gunther Piez; +Cc: gcc-help

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is is possible to use the name of a variable in the constructor?
  2005-08-01  0:04 ` Ian Lance Taylor
@ 2005-08-01  0:32   ` Gunther Piez
  2005-08-01  2:54     ` Ian Lance Taylor
  0 siblings, 1 reply; 7+ messages in thread
From: Gunther Piez @ 2005-08-01  0:32 UTC (permalink / raw)
  To: gcc-help

Am Montag, 1. August 2005 02:04 schrieb Ian Lance Taylor:
> Gunther Piez <gpiez@web.de> writes:
> > struct uniform {
> > 	uniform() {
> > 		cout << "A variable called " << __PRETTY_VARNAME__ << " was just
> > instantinated (sp)" << endl;
> > 	}
> > };
>
> 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.

Ok, I understand the difference to __PRETTY_FUNCTION__ now :-)
Maybe it is possible to use the debugging information somehow? At least the 
debugger usually knows the names of variables. Otherwise I have to use a 
constructor like

uniform::uniform(const char*);

and declare variables like

uniform blah("blah");

which is ugly.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is is possible to use the name of a variable in the constructor?
  2005-08-01  0:32   ` Gunther Piez
@ 2005-08-01  2:54     ` Ian Lance Taylor
  2005-08-01  4:13       ` Daniel Berlin
  0 siblings, 1 reply; 7+ messages in thread
From: Ian Lance Taylor @ 2005-08-01  2:54 UTC (permalink / raw)
  To: Gunther Piez; +Cc: gcc-help

Gunther Piez <gpiez@web.de> writes:

> Am Montag, 1. August 2005 02:04 schrieb Ian Lance Taylor:
> > Gunther Piez <gpiez@web.de> writes:
> > > struct uniform {
> > > 	uniform() {
> > > 		cout << "A variable called " << __PRETTY_VARNAME__ << " was just
> > > instantinated (sp)" << endl;
> > > 	}
> > > };

...

> Maybe it is possible to use the debugging information somehow? At least the 
> debugger usually knows the names of variables.

Maybe it is possible, but nothing straightforward comes to mind.
Remember that the debugger doesn't know the name within
uniform::uniform.  It only knows the name if you go up the stack frame
to the caller.

Ian

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is is possible to use the name of a variable in the constructor?
  2005-08-01  2:54     ` Ian Lance Taylor
@ 2005-08-01  4:13       ` Daniel Berlin
  2005-08-01 20:04         ` Gunther Piez
  0 siblings, 1 reply; 7+ messages in thread
From: Daniel Berlin @ 2005-08-01  4:13 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Gunther Piez, gcc-help

On Sun, 2005-07-31 at 19:53 -0700, Ian Lance Taylor wrote:
> Gunther Piez <gpiez@web.de> writes:
> 
> > Am Montag, 1. August 2005 02:04 schrieb Ian Lance Taylor:
> > > Gunther Piez <gpiez@web.de> writes:
> > > > struct uniform {
> > > > 	uniform() {
> > > > 		cout << "A variable called " << __PRETTY_VARNAME__ << " was just
> > > > instantinated (sp)" << endl;
> > > > 	}
> > > > };
> 
> ...
> 
> > Maybe it is possible to use the debugging information somehow? At least the 
> > debugger usually knows the names of variables.
> 
> Maybe it is possible, but nothing straightforward comes to mind.

You still have the issue that you need to have all the *actual* callers
in front of you, so you'd have to process debug info on the fly if you
were dlopen'ing stuff like plugins.

> Remember that the debugger doesn't know the name within
> uniform::uniform.  It only knows the name if you go up the stack frame
> to the caller.
> 
> Ian

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is is possible to use the name of a variable in the constructor?
  2005-07-31 23:56 Is is possible to use the name of a variable in the constructor? Gunther Piez
  2005-08-01  0:04 ` Ian Lance Taylor
@ 2005-08-01 11:29 ` Eljay Love-Jensen
  1 sibling, 0 replies; 7+ messages in thread
From: Eljay Love-Jensen @ 2005-08-01 11:29 UTC (permalink / raw)
  To: Gunther Piez, gcc-help

Hi Gunther,

The easiest way to implement your desired functionality is to make "passing
in the varname" a parameter, and by convention pass in the varname.

----------------------------------------------------------------
#include <iostream>
#include <string>

using std::cout;
using std::endl;

struct uniform {
  uniform(std::string const& prettyVarname) {
    cout << "A variable called \x1B[32m"
      << prettyVarname
      << "\x1B[0m was just instantiated (sp)"
      << endl;
    }
};

void main() {
    uniform blah("blah");
}
----------------------------------------------------------------

HTH,
--Eljay


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is is possible to use the name of a variable in the constructor?
  2005-08-01  4:13       ` Daniel Berlin
@ 2005-08-01 20:04         ` Gunther Piez
  0 siblings, 0 replies; 7+ messages in thread
From: Gunther Piez @ 2005-08-01 20:04 UTC (permalink / raw)
  To: gcc-help

Am Montag, 1. August 2005 06:12 schrieben Sie:
> On Sun, 2005-07-31 at 19:53 -0700, Ian Lance Taylor wrote:
> > Gunther Piez <gpiez@web.de> writes:
> > > Am Montag, 1. August 2005 02:04 schrieb Ian Lance Taylor:
> > > > Gunther Piez <gpiez@web.de> writes:
> > > > > struct uniform {
> > > > >       uniform() {
> > > > >               cout << "A variable called " << __PRETTY_VARNAME__ << 
" was just
> > > > > instantinated (sp)" << endl;
> > > > >       }
> > > > > };
> >
> > ...
> >
> > > Maybe it is possible to use the debugging information somehow? At least
> > > the debugger usually knows the names of variables.
> >
> > Maybe it is possible, but nothing straightforward comes to mind.
>
> You still have the issue that you need to have all the *actual* callers
> in front of you, so you'd have to process debug info on the fly if you
> were dlopen'ing stuff like plugins.

I thought it over: "uniform" needs to be a template anyway, and knowing the 
name during compile time is sufficient. So some macro will do.

It was late and I was a bit tired yesterday, sorry for the noise :-)

Thanks for the answers.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-08-01 20:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-31 23:56 Is is possible to use the name of a variable in the constructor? Gunther Piez
2005-08-01  0:04 ` Ian Lance Taylor
2005-08-01  0:32   ` Gunther Piez
2005-08-01  2:54     ` Ian Lance Taylor
2005-08-01  4:13       ` Daniel Berlin
2005-08-01 20:04         ` Gunther Piez
2005-08-01 11:29 ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).