lundi 12 août 2019

Return a variadic template of tuple

I would like to create a tuple from elements in vector and return it from my function. The size of a vector will not always be three so I need a function that can create a tuple from three, four and more elements and return it.

#include <iostream>
#include <tuple>
#include <vector>

template<typename... Args>
std::tuple<Args...> create_tuple(const std::vector<int>& vec) {
    if (vec.size() == 2)
        return std::make_tuple(vec[0], vec[1]);
    else if (vec.size() == 3)
        return std::make_tuple(vec[0], vec[1], vec[2]);
}

int main() {
    std::vector<int> vec{ 0, 1, 2 };
    auto t = create_tuple(vec);
}

Currently there is a compilation error, so how can I fix it? I am using C++11 and I cannot use 'auto' as a value type returned from the function.

Aucun commentaire:

Enregistrer un commentaire