digitalmars.D.learn - Extract template parameter at runtime?
- Yuxuan Shui (4/4) Jun 15 2015 I have a template class which is derived from a base class. Is it
- Steven Schveighoffer (6/9) Jun 15 2015 No. You can't get compile-time information at runtime, unless you have
- Yuxuan Shui (14/25) Jun 15 2015 Well I don't have a serious use case of this. I just started
- Yuxuan Shui (3/18) Jun 15 2015 Maybe I can put a virtual function in the class that return the
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/5) Jun 15 2015 Yeah, that's how I would do it.
- Baz (14/15) Jun 15 2015 yes, you can compare instance of TypeInfo using is or '==' too,
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
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
On Monday, 15 June 2015 at 18:30:55 UTC, Steven Schveighoffer wrote:On 6/15/15 2:10 PM, Yuxuan Shui 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.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
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:Maybe I can put a virtual function in the class that return the template parameter?[...]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
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
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