mercredi 24 juin 2020

C++ variadic template product

I'm writing a testbench for a library that operates on different container types i.e.

std::vector<int>
std::vector<char>
std::vector<bool>
std::list<int>
# etc

I'd like to automate the generation of tests by basically doing the cross product of std::vector, std::list, std::forward_list with <bool, char, int> and also have the tests generated in order of container type.

I was thinking of doing something w template packs:

template <class C> 
void register_containers() {
    std::string container_name = demangle(typeid(C).name());
    benchmark::RegisterBenchmark(container_name.c_str(), BM_Func, ArgObj());
}

template <class C, class C2, class... Args> 
void register_containers() {
    std::string container_name = demangle(typeid(C).name());
    benchmark::RegisterBenchmark(container_name.c_str(), BM_Func, ArgObj());
    register_containers<C2, Args...>();
}


template<class T> 
constexpr void register_types() {
    register_containers<std::list<T>, std::vector<T>>();
    return;
}

template <class T,class T2, class... Args> 
constexpr void register_types() {
    register_containers<std::list<T>, std::vector<T>>();
    register_types<T2, Args...>();
}

int main(int argc, char** argv) {
    register_types<unsigned char, unsigned short, unsigned int, unsigned long long int>();
    benchmark::Initialize(&argc, argv);
    benchmark::RunSpecifiedBenchmarks();
}

However doing it like this registers the benchmarks in order of the word type i.e. all char first, then all unsigned int etc. I would like to invert this, and specify the container type first and then iterate over word types for that container type. How do you provide container types like std::vector as a template parameter without also providing a type? Just passing in the std::vector gives me the "No matching function call to ..." error. The only hack I can think of is by providing the containers some dummy value type, and then rebinding them to the desired type later on.

Aucun commentaire:

Enregistrer un commentaire