Topic: How to use identifiers dynamically


Author: guthrie@miu.edu
Date: Fri, 18 Nov 94 14:22:02 CDT
Raw View
> Maybe you want:
>
> #define write_me_and_my_value(me) cout << #me << " == " (me)
>
> ("#" is the "stringify" operator in the preprocessor)
>
I have often thought of a similar capability;
  (to that in the original post)
The preprocessor defines certain syntactical constants relevant to the
lexical context, _FILE_, _LINE_, but no access to other lexical
information, notably the name of the current scope, or names of
declarations.

The basic idea is that there is important lexical information that is
not currently easily available to the program. I have thought of two
cases;

Static:
How about a compile-time static constant _NAME_ which would be set to
the current function, or object scope being compiled. This would allow
one to use this name in the body of the function for debugging,
messages, statistics, etc...

Dynamic:
Similarly, a secret run-time value _ID_ could be defined as a part of
any declaration (run-time); This would allow one to grab this name in
the constructor of an object. One can do similar things by the #
operator, and macros, but they all end up rather obscure and messy. It
there is really a value to this then why not 'really' support it?

There's several details not speficied above (name of global scope,
nested scopes, etc.) (names of anonymous objects), but if the idea is
useful these would be easy.

Yes?






Author: guthrie@miu.edu
Date: Fri, 18 Nov 94 14:37:30 CDT
Raw View
> >ostream& X::whoami(ostream& s)
> >{
> >  return s << "My name is " << ??????;
> >    ^^^^^
> >    what I want to know
> >}
>
> Maybe you want:
>
> #define write_me_and_my_value(me) cout << #me << " == " (me)
>
> ("#" is the "stringify" operator in the preprocessor)

Can one "stringify" template arguments;
so that e.g. one could say:

template < class Type >
void Coll<Type>::identify() {
    cout <<  "I am a collection of type:" << #Type ;
    }

Where is the rule defining if this is/not possible?




Author: pjl@graceland.att.com (Paul J. Lucas)
Date: Sat, 19 Nov 1994 01:25:08 GMT
Raw View
In <3aj3p2$hv1@news.iastate.edu> guthrie@miu.edu writes:

>> >ostream& X::whoami(ostream& s)
>> >{
>> >  return s << "My name is " << ??????;
>> >    ^^^^^
>> >    what I want to know
>> >}
>>
>> Maybe you want:
>>
>> #define write_me_and_my_value(me) cout << #me << " == " (me)
>>
>> ("#" is the "stringify" operator in the preprocessor)

>Can one "stringify" template arguments;
>so that e.g. one could say:
>
>template < class Type >
>void Coll<Type>::identify() {
>    cout <<  "I am a collection of type:" << #Type ;
>    }

 No.

>Where is the rule defining if this is/not possible?

 cpp doesn't know anything about C++ (and very little about C).

 BTW: Despite Bjarne's dislike of the preprocessor, I (and
 others) often wish there were a C++-savvy cpp (or should that be
 c++pp?) so that you could do things like that.
--
 - Paul J. Lucas
   AT&T Bell Laboratories
   Naperville, IL




Author: john_werner@taligent.com (John Werner)
Date: Sat, 19 Nov 1994 05:49:27 GMT
Raw View
> >template < class Type >
> >void Coll<Type>::identify() {
> >    cout <<  "I am a collection of type:" << #Type ;
> >    }

There is a way to do this, if you have a compiler which supports RTTI (and
assuming I have an up-to-date description of RTTI):

template <class Type>
void Coll<Type>::identify() {
    cout << "I am a collection of type:" << typeid(Type).name();
}

--
John Werner                           john_werner@taligent.com
Taligent, Inc.




Author: jason@cygnus.com (Jason Merrill)
Date: Sat, 19 Nov 1994 04:41:07 GMT
Raw View
>>>>> guthrie  <guthrie@miu.edu> writes:

> Can one "stringify" template arguments;
> so that e.g. one could say:

> template < class Type >
> void Coll<Type>::identify() {
>     cout <<  "I am a collection of type:" << #Type ;
>     }

Try

template <class Type>
void Coll<Type>::identify() {
  cout << "I am a collection of type:" << typeid(Type).name();
}

instead...

Jason




Author: guthrie@miu.edu
Date: Sat, 19 Nov 94 07:18:11 CDT
Raw View

In article <3aj3p2$hv1@news.iastate.edu>, <guthrie@miu.edu> writes:

> Can one "stringify" template arguments;
OOps!
Obviously I mean to say::   "should" one be able to....
> so that e.g. one could say:
>
> template < class Type >
> void Coll<Type>::identify() {
>     cout <<  "I am a collection of type:" << #Type ;
>     }
>
After all,
templates are a compile time phenomena,
and with the old macro-based approaches, this would have been easy.




Author: marnold@netcom.com (Matt Arnold)
Date: Sun, 20 Nov 1994 04:31:43 GMT
Raw View
guthrie@miu.edu writes:



>In article <3aj3p2$hv1@news.iastate.edu>, <guthrie@miu.edu> writes:

>> Can one "stringify" template arguments;
>OOps!
>Obviously I mean to say::   "should" one be able to....
>> so that e.g. one could say:
>>
>> template < class Type >
>> void Coll<Type>::identify() {
>>     cout <<  "I am a collection of type:" << #Type ;
>>     }
>>
>After all,
>templates are a compile time phenomena,
>and with the old macro-based approaches, this would have been easy.

Sure, and it's still easy with RTTI, at run-time too.

If you compiler does not support RTTI, chuck it.  No seriously..., just
add a parameter to your template containing the name...

template <class Type, char* Name>
void Coll<Type, Name>::identify() {
    cout << "I am a collection of type:" << Name;
    }

...and use it like this...

    Coll<Foo, "Foo"> fooColl;

...and even make an old-style macro, if you like...

#define OF_TYPE(Type) Type, #Type

...and use it...

    Coll<OF_TYPE(Foo)> fooColl;

------------------------------------------------------------------------------
Matt Arnold                         |        | ||| | |||| |  | | || ||
marnold@netcom.com                  |        | ||| | |||| |  | | || ||
Boston, MA                          |      0 | ||| | |||| |  | | || ||
617.389.7384 (H) 617.576.2760 (W)   |        | ||| | |||| |  | | || ||
Windoz..z..Z..Z..e C++/C developer  |        | ||| 4 3 1   0 8 3 || ||
------------------------------------------------------------------------------




Author: guthrie@miu.edu
Date: Sun, 20 Nov 94 16:10:05 CDT
Raw View
In article <marnoldCzJtww.MyF@netcom.com>, <marnold@netcom.com> writes:

> >> so that e.g. one could say:
> >>
> >> template < class Type >
> >> void Coll<Type>::identify() {
> >>     cout <<  "I am a collection of type:" << #Type ;
> >>     }

> add a parameter to your template containing the name...
>
> template <class Type, char* Name>
> void Coll<Type, Name>::identify() {
>     cout << "I am a collection of type:" << Name;
>     }
> ....and use it like this...
>     Coll<Foo, "Foo"> fooColl;
>
> ....and even make an old-style macro, if you like...
> #define OF_TYPE(Type) Type, #Type
>     Coll<OF_TYPE(Foo)> fooColl;
Yes, but..
Here the user is required to explicitly pass something,
and fool with and pass the name;
better if the routine internally could deduce this,
after all, this is easier,
less error prone, and keeps the fact that it wants this name private.

You show how to accomplish the goal without a supporting feature;
yes it can be done, but this was just one example to show the value of
a proposed feature.
The above aproach just says "since I can't figure out the name,
you must specify it and pass it in",
I was proposing an ability to avoid this..

Of course this is just one example; the main idea was that this is
indeed useful information, and it would be easy to make it available.
>





Author: mikey@boatman.mikey.pr.mcs.net (Michael H. Young)
Date: 20 Nov 1994 23:05:05 GMT
Raw View
In article <3aku47$4rp@news.iastate.edu> guthrie@miu.edu writes:


   In article <3aj3p2$hv1@news.iastate.edu>, <guthrie@miu.edu> writes:

   > Can one "stringify" template arguments;
   OOps!
   Obviously I mean to say::   "should" one be able to....
   > so that e.g. one could say:
   >
   > template < class Type >
   > void Coll<Type>::identify() {
   >     cout <<  "I am a collection of type:" << #Type ;
   >     }
   >
   After all,
   templates are a compile time phenomena,
   and with the old macro-based approaches, this would have been easy.

-------------
 Assuming that Type was polymorphic, you could simply create an
instance of Type, and use RTTI. You might even be able to get the type
info from a class directly, or get at the vtbl and somehow find it's
type info there. This won't work with base types or non-polymorphic
classes.

These are run-time phenomena, but you have what you have.

Mike.




Author: reycri@atlantis.actrix.gen.nz (Reynaldo Chrisostono)
Date: Tue, 22 Nov 1994 08:16:39 GMT
Raw View
In article <3aetf8$obb@uqcspe.cs.uq.oz.au>,
Warwick Allison <warwick@cs.uq.oz.au> wrote:
> shark@hdtv (Seehyun Kim) writes:
>
> >ostream& X::whoami(ostream& s)
> >{
> >  return s << "My name is " << ??????;
> >    ^^^^^
> >    what I want to know
> >}
>
> Maybe you want:
>
> #define write_me_and_my_value(me) cout << #me << " == " (me)
>
>
> ("#" is the "stringify" operator in the preprocessor)
>
>
> BTW,
> main() { (new X)->whoami(cout)?  }
> ????  The idea is nutty.
>
> --
> Warwick
> --
>   _-_|\      warwick@cs.uq.oz.au            /     Microsoft is not the answer.
>  /     * <-- Computer Science Department,  /     Microsoft is the question.
>  \_.-._/     University of Queensland,    /
>       v      Brisbane, Australia.        /     NO is the answer.

This will only work if you want to generate your variables at compile
time. In which case, you're better off using a function template.

Rey Crisostomo
Wellington, New Zealand

--
Rey Crisostomo
reycri@actrix.gen.nz
Wellington, New Zealand





Author: jones@cais.cais.com (Ben Jones)
Date: 22 Nov 1994 16:22:20 GMT
Raw View
Paul J. Lucas (pjl@graceland.att.com) wrote:
: In <3aj3p2$hv1@news.iastate.edu> guthrie@miu.edu writes:

: >> >ostream& X::whoami(ostream& s)
: >> >{
: >> >  return s << "My name is " << ??????;
: >> >    ^^^^^
: >> >    what I want to know
: >> >}
: >>
: >> Maybe you want:
: >>
: >> #define write_me_and_my_value(me) cout << #me << " == " (me)
: >>
: >> ("#" is the "stringify" operator in the preprocessor)

: >Can one "stringify" template arguments;
: >so that e.g. one could say:
: >
: >template < class Type >
: >void Coll<Type>::identify() {
: >    cout <<  "I am a collection of type:" << #Type ;
: >    }

:  No.

: >Where is the rule defining if this is/not possible?

:  cpp doesn't know anything about C++ (and very little about C).

:  BTW: Despite Bjarne's dislike of the preprocessor, I (and
:  others) often wish there were a C++-savvy cpp (or should that be
:  c++pp?) so that you could do things like that.
: --
:  - Paul J. Lucas
:    AT&T Bell Laboratories
:    Naperville, IL

The C-preprocessor is primitive even by the standards of macro facilities in
assemblers which were available when it was first developed.

What is needed is the ability to define macros as members of classes,
give them looping, branching, and error reporting capabilities, and then
let them access the C++ parse tree/symbol table through an "attribute"
syntax.

I would suggest something like:

    $macro name(args) { text }
    $if (const-expression) { text1} $else { text2 }
    $for name=list { text }

and an attribute mechanism like:

    item @ attribute @ sub-attribute ...

where attributes could be names, lists of symbols (to be decoded by
$for), type codes, etc.

Certain macros would be invoked automatically at various places in the
compilation process such as at the end of a class declaration so that you
could generate support code based on the membership list of that class.
The inheritance of base classes containing such "callback macros" would
guarantee that the appropriate source code could be generated for all
derived classes.

This scheme has been implemented in a preprocessor which piggybacks onto
C++.  For more information, please contact me.

Ben Jones
ARSoftware Corporation
jones@arsoftware.arclch.com




Author: zhou@tidmmpl.csc.ti.com (Tonghang Zhou)
Date: 17 Nov 1994 04:31:57 GMT
Raw View
In article <1994Nov16.010436.20173@news.snu.ac.kr>,
Seehyun Kim <shark@hdtv> wrote:
>Is there any way to use identifiers(i.e., names of variables)
>dynamically? Although we give a specific name to a variable,
>the program recognize it not by its name, but by an internal name,
>such as an index of symbol table. What I want is how to use the
>identifier that I gave inside the program. For example,
> ...
>{
>  X alpha;
>  alpha.whoami(cout);
>}
>I hope it produce:
>  My name is alpha
>I'd appreciate any help/suggestion/comments.

 This can not be done: your alpha, eg, is only an allocation
 on the run time stack, no name is associated:

   ; enter function
   push parameters
   push return address
   set base pointer to stack pointer
   subtract stack pointer by size of alpha

   ; from here on alpha is known as the location
   ; pointed to by the base pointer

 Then of course, you can write your own C++ compiler.
--
 Regards, Tonghang
________________________________________________________________________________
Tonghang Zhou ("Zhou" is pronounced "Joe")
zhou@daLdd.sc.ti.com, W:(214)997-3933, H:(214)458-7565, IMS ID THZH, M/S 8316




Author: warwick@cs.uq.oz.au (Warwick Allison)
Date: 17 Nov 1994 06:33:11 GMT
Raw View
shark@hdtv (Seehyun Kim) writes:

>ostream& X::whoami(ostream& s)
>{
>  return s << "My name is " << ??????;
>    ^^^^^
>    what I want to know
>}

Maybe you want:

#define write_me_and_my_value(me) cout << #me << " == " (me)


("#" is the "stringify" operator in the preprocessor)


BTW,
main() { (new X)->whoami(cout)?  }
????  The idea is nutty.

--
Warwick
--
  _-_|\      warwick@cs.uq.oz.au            /     Microsoft is not the answer.
 /     * <-- Computer Science Department,  /     Microsoft is the question.
 \_.-._/     University of Queensland,    /
      v      Brisbane, Australia.        /     NO is the answer.




Author: shark@hdtv (Seehyun Kim)
Date: Wed, 16 Nov 94 01:04:36 GMT
Raw View
Hi there,

Is there any way to use identifiers(i.e., names of variables)
dynamically? Although we give a specific name to a variable,
the program recognize it not by its name, but by an internal name,
such as an index of symbol table. What I want is how to use the
identifier that I gave inside the program. For example,

class X
{
..
ostream& whoami(ostream&);
..
}

ostream& X::whoami(ostream& s)
{
  return s << "My name is " << ??????;
    ^^^^^
    what I want to know
}

main()
{
  X alpha;

  alpha.whoami(cout);
}

I hope it produce:

  My name is alpha

I'd appreciate any help/suggestion/comments.

Seehyun Kim