Struct that contains an alias to the first convertible type of a variadic pack

Viewed 136

I have typename T1 and I have a parameter pack typename... Variadic.

I want to create a struct that contains a using alias using Type = ... to the first type in the parameter pack that T1 can be converted into. So far I have tried the following:

template<typename T1, typename T2, typename... Variadic>
struct VariadicConvertibleType
{
    using Type = std::enable_if<std::is_convertible<T1, T2>::value, T2>::type;
};

This could be a potential solution using SFINAE for the first two types, but I need to expand this to all the types in the pack using recursion. All my attempts failed so far, because you can't put conditionals into using alias declarations. Otherwise something similar to this could be used:

template<typename T1, typename T2, typename... Variadic>
struct VariadicConvertibleType
{
    using Type = std::is_convertible<T1, T2>::value ? T2 : VariadicConvertibleType<T1, Variadic...>::Type;
};

I can use everything up to (and including) C++14 to implement the solution. I can not using anything other than the standary library though.

2 Answers
Related