Traits
Agustin
agustin.l.alvarez at hotmail.com
Fri Oct 11 14:09:13 PDT 2013
On Friday, 11 October 2013 at 19:19:31 UTC, luminousone wrote:
> On Friday, 11 October 2013 at 14:09:09 UTC, Gary Willoughby
> wrote:
>> 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));
>> }
>
> Using casts that way won't always be correct, it would be
> better to use reflection in some way if possible.
This is wrong for me because i don't have a value, i just wanted
to check if a template parameter inherit a class at compile time.
bool instanceOf(A, B)();
More information about the Digitalmars-d-learn
mailing list