samedi 4 juin 2016

How to specialize template pack?

I have the following code, which tries to convert a binary number (passed as a list of booleans, least-significant first, variable lenght) into a decimal number:

#include <iostream>
using namespace std;

template<typename T>
int bin_to_dec(int multi, T first) {
    cout<<"mutli"<<multi<<endl;
  return first?multi:0;
}

template<typename T, typename... Args>
int bin_to_dec(int multi, T first, Args... args) {
    cout<<"mutli"<<multi<<endl;
  return (first?multi:0) + adder(multi*2, args...);
}

template<typename T, typename... Args>
int bin_to_dec(T first, Args... args) {
    cout<<"mutli"<<1<<endl;
  return (first?1:0) + adder(2, args...);
}

int main()
{
    cout<<bin_to_dec(true, true, false, true)<<endl;
}

It works quite well, but I would like to make it possible only for booleans, so when I try something like bin_to_dec(1,2,3) it should not compile. I was trying to use something like

template<bool First, bool... Bools>

but I can't figure out how to go further with that. Any ideas?

Aucun commentaire:

Enregistrer un commentaire