www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Extract template parameter at runtime?

reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
I have a template class which is derived from a base class. Is it 
possible to extract the template parameter from a reference to 
the base class?

Can is() operate on TypeInfo?
Jun 15 2015
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/15/15 2:10 PM, Yuxuan Shui wrote:
 I have a template class which is derived from a base class. Is it
 possible to extract the template parameter from a reference to the base
 class?
No. You can't get compile-time information at runtime, unless you have stored it somewhere that runtime can read. But perhaps you can further describe your requirement, and a solution might be available. -Steve
Jun 15 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Monday, 15 June 2015 at 18:30:55 UTC, Steven Schveighoffer 
wrote:
 On 6/15/15 2:10 PM, Yuxuan Shui wrote:
 I have a template class which is derived from a base class. Is 
 it
 possible to extract the template parameter from a reference to 
 the base
 class?
No. You can't get compile-time information at runtime, unless you have stored it somewhere that runtime can read. But perhaps you can further describe your requirement, and a solution might be available. -Steve
Well I don't have a serious use case of this. I just started using D couple of weeks ago, and is now experimenting with it by writing a toy compiler. What I'm doing is I'm abusing the D type system to represent types in my toy language. I store type information into template parameters, and when I need to type check, I just use typeid and compare the result. Now I want to generate different code when I encounter different types, and for that I want to get the template parameters. Of course if this is not possible I can always go back to implement my own type system properly. It's just a good thing to have.
Jun 15 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Monday, 15 June 2015 at 22:56:57 UTC, Yuxuan Shui wrote:
 On Monday, 15 June 2015 at 18:30:55 UTC, Steven Schveighoffer 
 wrote:
 [...]
Well I don't have a serious use case of this. I just started using D couple of weeks ago, and is now experimenting with it by writing a toy compiler. What I'm doing is I'm abusing the D type system to represent types in my toy language. I store type information into template parameters, and when I need to type check, I just use typeid and compare the result. Now I want to generate different code when I encounter different types, and for that I want to get the template parameters. Of course if this is not possible I can always go back to implement my own type system properly. It's just a good thing to have.
Maybe I can put a virtual function in the class that return the template parameter?
Jun 15 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/15/2015 04:04 PM, Yuxuan Shui wrote:

 Maybe I can put a virtual function in the class that return the template
 parameter?
Yeah, that's how I would do it. Ali
Jun 15 2015
prev sibling parent "Baz" <bb.temp gmx.com> writes:
On Monday, 15 June 2015 at 18:10:34 UTC, Yuxuan Shui wrote:
 Can is() operate on TypeInfo?
yes, you can compare instance of TypeInfo using is or '==' too, using typeid which is returns at run-time the TypeInfo of the argument: --- void main() { assert(typeid(2) == typeid(1)); assert(typeid(2) is typeid(1)); Object a = new Object; Object b = new Object; assert(typeid(b) is typeid(a)); } ---
Jun 15 2015