lundi 20 mai 2019

I want strip argument from variable args by using template meta programming

I am new to template meta programming. I want to strip args from variable argument in c++. I am making a function which will push_back element to any type of container. Its very easy to do in c++ 17 but i want to provide support for C++ 11. Please find the code below.Please avoid va_start(), va_end() c style solution.


#include <iostream>
#include <vector>
template<class Container, class T, class... Args>
void push_back(Container& con, T tail, Args... args);

template<class T>
T get_tail(T data) {
    return data;
}

template<class T, class ...Args>
T get_tail(T& tail, Args&... args) {
    return tail;
}

template<class Container , class T,class... Args>
void push_back(Container& con, T tail,Args... args ) {
        //C++ 17 ((con.push_back(args), ...);
    con.push_back(tail);
    std::cout << (tail) << std::endl;
    T newTail = get_tail(args...);
    push_back(con,newTail,args...); 
}

template<typename T, typename... Args>
bool pair_comparer(T a, T b, Args... args) {
    return a == b && pair_comparer(args...);
}

int main()
{
    std::vector<int> v_int;
    push_back(v_int,1,2,3,4 );
  std::cout << "Hello World!\n";
    for (auto iter = v_int.begin(); iter != v_int.begin(); iter++) {
        std::cout << "=== " << *iter << " ===" << std::endl;
    }
}

Aucun commentaire:

Enregistrer un commentaire