Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 15, 2015 Extract template parameter at runtime? | ||||
---|---|---|---|---|
| ||||
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? |
June 15, 2015 Re: Extract template parameter at runtime? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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
|
June 15, 2015 Re: Extract template parameter at runtime? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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));
}
---
|
June 15, 2015 Re: Extract template parameter at runtime? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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.
|
June 15, 2015 Re: Extract template parameter at runtime? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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?
|
June 15, 2015 Re: Extract template parameter at runtime? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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
|
Copyright © 1999-2021 by the D Language Foundation