簡體   English   中英

select 如何從 C++ 中的幾種類型的元組中提取多個元素?

[英]how select multiple elements from a tuple by several types in C++?

這是我的代碼, 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); }

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


 auto a = df.select<int,bool>();

此模板 function 的參數是intbool

但很明顯,您的data元組包含其中的std::vector ,因此您的select()方法應該是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM