Thread overview | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 11, 2013 Traits | ||||
---|---|---|---|---|
| ||||
I have a function that needs to check if the template provided inherit a class. For example: public void function(T, A...)(auto ref A values) { // static assert(IsBaseOf(L, T)); } Check if T inherit class "L". Same result that std::is_base_of<L, T>::value using C++. Any clean way to do it, without a dirty hack. |
October 11, 2013 Re: Traits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | On 10/10/2013 09:13 PM, Agustin wrote: > I have a function that needs to check if the template provided inherit a > class. > > For example: > > public void function(T, A...)(auto ref A values) function happens to be a keyword. :) > { > // static assert(IsBaseOf(L, T)); > } > > Check if T inherit class "L". Same result that std::is_base_of<L, > T>::value using C++. Any clean way to do it, without a dirty hack. One of the uses of the is expression determines "whether implicitly convertible to". It may work for you: public void foo(T, A...)(auto ref A values) { static assert(is (T : L)); } class L {} class LL : L {} void main() { foo!LL(1, 2.3, "4"); } Ali |
October 11, 2013 Re: Traits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Thursday, October 10, 2013 21:35:37 Ali Çehreli wrote:
> One of the uses of the is expression determines "whether implicitly convertible to". It may work for you:
>
> public void foo(T, A...)(auto ref A values)
> {
> static assert(is (T : L));
> }
Actually, checking for implicit conversion will work directly in the template signature without a template constraint or static assertion. e.g.
public void foo(T : L, A...)auto ref A values)
{
}
- Jonathan M Davis
|
October 11, 2013 Re: Traits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | On Friday, 11 October 2013 at 04:13:55 UTC, Agustin wrote:
> I have a function that needs to check if the template provided inherit a class.
>
> For example:
>
> public void function(T, A...)(auto ref A values)
> {
> // static assert(IsBaseOf(L, T));
> }
>
> Check if T inherit class "L". Same result that std::is_base_of<L, T>::value using C++. Any clean way to do it, without a dirty hack.
import std.traits;
bool ChildInheritsFromParent( parent, child )( ) {
foreach ( k, t; BaseClassesTuple!child ) {
if( typeid(t) == typeid(parent) )
return true;
}
return false;
}
|
October 11, 2013 Re: Traits | ||||
---|---|---|---|---|
| ||||
Posted in reply to luminousone | On 2013-10-11 07:49, luminousone wrote: > import std.traits; > > bool ChildInheritsFromParent( parent, child )( ) { > > foreach ( k, t; BaseClassesTuple!child ) { > if( typeid(t) == typeid(parent) ) > return true; > } > return false; > } That will perform a runtime check and not a compile time check. -- /Jacob Carlborg |
October 11, 2013 Re: Traits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Friday, 11 October 2013 at 09:37:33 UTC, Jacob Carlborg wrote:
> On 2013-10-11 07:49, luminousone wrote:
>
>> import std.traits;
>>
>> bool ChildInheritsFromParent( parent, child )( ) {
>>
>> foreach ( k, t; BaseClassesTuple!child ) {
>> if( typeid(t) == typeid(parent) )
>> return true;
>> }
>> return false;
>> }
>
> That will perform a runtime check and not a compile time check.
Is is just the typeid call that makes it unable to be ran at compile time or is their something else wrong in their?,
Would a string compare with type.classInfo.name fix that, or is their not a tool yet in place for that?
|
October 11, 2013 Re: Traits | ||||
---|---|---|---|---|
| ||||
Posted in reply to luminousone | On 2013-10-11 11:49, luminousone wrote: > Is is just the typeid call that makes it unable to be ran at compile > time or is their something else wrong in their?, > > Would a string compare with type.classInfo.name fix that, or is their > not a tool yet in place for that? Hmm, it may actually be possible to run this at compile time. I don't remember. I was thinking that "typeid" doesn't work at compile time, but I might be wrong. -- /Jacob Carlborg |
October 11, 2013 Re: Traits | ||||
---|---|---|---|---|
| ||||
Posted in reply to luminousone | On Friday, 11 October 2013 at 05:49:38 UTC, luminousone wrote:
> On Friday, 11 October 2013 at 04:13:55 UTC, Agustin wrote:
>> I have a function that needs to check if the template provided inherit a class.
>>
>> For example:
>>
>> public void function(T, A...)(auto ref A values)
>> {
>> // static assert(IsBaseOf(L, T));
>> }
>>
>> Check if T inherit class "L". Same result that std::is_base_of<L, T>::value using C++. Any clean way to do it, without a dirty hack.
>
> import std.traits;
>
> bool ChildInheritsFromParent( parent, child )( ) {
>
> foreach ( k, t; BaseClassesTuple!child ) {
> if( typeid(t) == typeid(parent) )
> return true;
> }
> return false;
> }
A simpler way:
import std.stdio;
bool instanceOf(A, B)(B value)
{
return !!cast(A)value;
}
void main()
{
assert(1.instanceOf!(int));
}
|
October 11, 2013 Re: Traits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Friday, 11 October 2013 at 04:35:38 UTC, Ali Çehreli wrote: > On 10/10/2013 09:13 PM, Agustin wrote: > > > I have a function that needs to check if the template > provided inherit a > > class. > > > > For example: > > > > public void function(T, A...)(auto ref A values) > > function happens to be a keyword. :) > > > { > > // static assert(IsBaseOf(L, T)); > > } > > > > Check if T inherit class "L". Same result that > std::is_base_of<L, > > T>::value using C++. Any clean way to do it, without a dirty > hack. > > One of the uses of the is expression determines "whether implicitly convertible to". It may work for you: > > public void foo(T, A...)(auto ref A values) > { > static assert(is (T : L)); > } > > class L > {} > > class LL : L > {} > > void main() > { > foo!LL(1, 2.3, "4"); > } > > Ali On Friday, 11 October 2013 at 05:45:00 UTC, Jonathan M Davis wrote: > On Thursday, October 10, 2013 21:35:37 Ali Çehreli wrote: >> One of the uses of the is expression determines "whether implicitly >> convertible to". It may work for you: >> >> public void foo(T, A...)(auto ref A values) >> { >> static assert(is (T : L)); >> } > > Actually, checking for implicit conversion will work directly in the template > signature without a template constraint or static assertion. e.g. > > public void foo(T : L, A...)auto ref A values) > { > } > > - Jonathan M Davis Those work great, thanks a lot! |
October 11, 2013 Re: Traits | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Friday, October 11, 2013 15:34:28 Jacob Carlborg wrote:
> On 2013-10-11 11:49, luminousone wrote:
> > Is is just the typeid call that makes it unable to be ran at compile time or is their something else wrong in their?,
> >
> > Would a string compare with type.classInfo.name fix that, or is their not a tool yet in place for that?
>
> Hmm, it may actually be possible to run this at compile time. I don't remember. I was thinking that "typeid" doesn't work at compile time, but I might be wrong.
If typeid works at compile time, it's only because you're using CTFE. It returns the type of the instance when the code runs, not statically. So, it's intended as a runtime check, not a compile time check.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation