| Thread overview |
|---|
June 27, 2017 isImplictlyConvertible for Variadic Templates | ||||
|---|---|---|---|---|
| ||||
I was wondering whether there was a method built into phobos for expanding the "isImplictlyConvertible" template to work for variadic templates.
To explain what I mean by this, normally "isImplicitlyConvertible(To, From)" checks whether or not a type "From" may implicitly be converted to a type "To".
struct Foo(V)
{
this(V)(V value) if (isImplictlyConvertible!(V, T))
{
// do stuff
}
}
When "From" is variadic, I would like it for each type in "From" to be checked whether it may implicitly be converted to type "To".
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.
template eachIsImplictlyConvertible(To, From...)
{
enum bool eachIsImplictlyConvertible = is(typeof({
void fun(ref From vs)
{
void gun(To) {}
foreach (v; vs) // +
{
gun(v);
}
}
}));
}
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.
Thanks,
rpeio
| ||||
June 27, 2017 Re: isImplictlyConvertible for Variadic Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to rpeio | 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 } ---- | |||
June 27, 2017 Re: isImplictlyConvertible for Variadic Templates | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Tuesday, 27 June 2017 at 01:20:01 UTC, ag0aep6g wrote:
> ----
> import std.meta: allSatisfy, ApplyRight;
> import std.traits: isImplicitlyConvertible;
>
> this(Vs...)(Vs values)
> if (allSatisfy!(ApplyRight!(isImplicitlyConvertible, T), Vs))
> {
> // do stuff
> }
> ----
That is exactly what I was looking for. I was unaware of the existence of ApplyRight and ApplyLeft. Thank you for your help.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply