isImplictlyConvertible for Variadic Templates

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 26 18:20:01 PDT 2017


On 06/27/2017 02:59 AM, rpeio wrote:
> struct Foo(V)
> {
>      this(Vs...)(Vs values) if (eachIsImplictlyConvertible!(T, Vs))
>      {
>      // do stuff
>      }
> }
> 
> This can be accomplished off the top of my head by taking the code from 
> std.traits for "isImplicitlyConvertible" and making the following change.
> 
[...]
> 
> Implementing this means I have an ostensibly valid method of 
> accomplishing what I desire. Though, I am curious whether there's a way 
> to facilitate this that is currently available in phobos, without having 
> to implement another traits function.

----
     import std.meta: allSatisfy, ApplyRight;
     import std.traits: isImplicitlyConvertible;

     this(Vs...)(Vs values)
         if (allSatisfy!(ApplyRight!(isImplicitlyConvertible, T), Vs))
     {
         // do stuff
     }
----

Though, for implicit conversion of all arguments to some type T you can 
also use this variant of variadic functions:

----
     this(T[] values ...)
     {
         // do stuff
     }
----


More information about the Digitalmars-d-learn mailing list