Traits

Agustin agustin.l.alvarez at hotmail.com
Fri Oct 11 07:45:05 PDT 2013


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!


More information about the Digitalmars-d-learn mailing list