ztik
ztik

Reputation: 3612

Variadic convertible check for arguments in a class

I wish to create a class that will check two list of argument types to see if all the arguments of the first list can be casted to the second list. So far I have something like this:

template <typename OArg> 
class conv{
  public:
    template<typename IArg>
    static bool check(){
      return std::is_convertible<IArg,OArg>::value;
    }
};

template <typename OArg, typename... OArgs>
class conv{
  public:
    template<typename IArg, typename... IArgs>
    static bool check(){
      return Op<OArg>::check<IArg>() && Op<OArgs...>::check<IArgs...>();
    }
};

I want to use it like this:

bool pass = conv<char,a_class,float>::check<float,int,b_class>();

When compiling I get:

recursive.cpp:19:7: error: redeclared with 2 template parameters
 class Conv{
       ^
recursive.cpp:10:7: note: previous declaration ‘template<class OArg> class Conv’ used 1 template parameter
 class Conv{

At check time I have no instances of either IArgs or OArgs Could you suggest any solutions?

Upvotes: 0

Views: 120

Answers (1)

T.C.
T.C.

Reputation: 137301

You can't overload class templates. You can partially specialize them, but there's no need to do that here. Borrowing @Columbo's bool_pack trick:

template<bool...> struct bool_pack;
template<bool... b> 
using all_true = std::is_same<bool_pack<true, b...>, bool_pack<b..., true>>;

template <typename... OArgs>
class conv{
  public:
    template<typename... IArgs>
    static bool check(){
      return all_true<std::is_convertible<IArgs, OArgs>::value...>::value;
    }
};

Upvotes: 2

Related Questions