mercredi 22 février 2017

Retrieve special type parameter of recursive variadic template

Is something like that possible:

#include <iostream>
#include <string>

template<typename T1, typename T2, typename T3, typename T4, class ...Ts>
struct third_t {

    typedef typename third_t<T2,T3,T4,Ts...>::type type;
};

template<typename T1,typename T2, typename T3>
struct third_t<T1,T2,T3,void> {

    typedef T3 type;
};

int main() {

    third_t<int,int,std::string,unsigned>::type z = "test";

    std::cout<<"string type: "<<z<<std::endl;
}

If yes, why isn't the specialized case found? I.e. it isn't even considered in the resolution.

I get the error:

main.cpp: In instantiation of 'struct third_t<int, int, std::__cxx11::basic_string<char>, unsigned int>':

main.cpp:18:46:   required from here

main.cpp:7:56: error: wrong number of template arguments (3, should be at least 4)

     typedef typename third_t<T2,T3,T4,Ts...>::type type;

Aucun commentaire:

Enregistrer un commentaire