Как выбрать несколько элементов из кортежа по нескольким типам в C ++? - PullRequest
0 голосов
/ 22 февраля 2020

Вот мой код, a должен получить переменную типа std::tuple<int,bool>. Но это не работает. Итак, что не так и как это исправить?

#include <vector>
#include <tuple>

template <class... Ts>
class vector_df {
public:
    std::tuple<std::vector<Ts>...> data;

    template <class... As>
    auto select() {
        return std::make_tuple(std::get<As>(data)...);
    }
};

int main() {
    vector_df<int,char,bool> df;
    auto a = df.select<int,bool>();
    return 0;
}

Вот ссылка на код в онлайновой C ++ IDE. https://godbolt.org/z/BwzvCZ Сообщение об ошибке:

/opt/compiler-explorer/gcc-9.1.0/include/c++/9.1.0/tuple: In instantiation of 'constexpr _Tp& std::get(std::tuple<_Elements ...>&) [with _Tp = int; _Types = {std::vector<int, std::allocator<int> >, std::vector<char, std::allocator<char> >, std::vector<bool, std::allocator<bool> >}]':

<source>:11:38:   required from 'auto vector_df<Ts>::select() [with As = {int, bool}; Ts = {int, char, bool}]'

<source>:17:34:   required from here

/opt/compiler-explorer/gcc-9.1.0/include/c++/9.1.0/tuple:1365:37: error: no matching function for call to '__get_helper2<int>(std::tuple<std::vector<int, std::allocator<int> >, std::vector<char, std::allocator<char> >, std::vector<bool, std::allocator<bool> > >&)'

 1365 |     { return std::__get_helper2<_Tp>(__t); }

      |              ~~~~~~~~~~~~~~~~~~~~~~~^~~~~


1 Ответ

3 голосов
/ 22 февраля 2020
 auto a = df.select<int,bool>();

Ваши параметры для этой функции шаблона int и bool.

Но очевидно, что ваш data кортеж содержит std::vector из них, поэтому ваш select() метод должно быть:

template <class... As>
auto select() {
    return std::make_tuple(std::get<std::vector<As>>(data)...);
}
...