在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>();
此模板函数的参数为int
和bool
。
但很明显,您的data
元组包含其中的std::vector
s,因此您的select()
方法应该是:
template <class... As>
auto select() {
return std::make_tuple(std::get<std::vector<As>>(data)...);
}
相关文章:
- C++:TypeDef使用元组
- Pybind11:将元组列表从Python传递到C++
- 重载元组索引运算符-C++
- 在C++中,如何通过几种类型从元组中选择多个元素
- 将fold表达式与std::一起用于两个元组
- std::ranges::elements_view,用于自定义类似元组的数据
- 将元组的向量转换/构造为堆
- 专用于 std 元组的模板,而无需用户执行remove_cvref
- 将元组的向量构造成堆
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 将元组类型扩展为可变参数模板?
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- 类内部和外部静态 constexpr 元组之间的差异
- 可变参数模板与使用元组在参数中添加不同的数据对
- 访问和打印元组中的数据,并使用 C++14 使用模板函数显示数据
- boost::包含提升单元的元组的哈希值
- 如何映射数组元素以选择二进制树节点
- C 通过随机选择其元素从两个元组制成元组
- 可变模板:选择具有适当方法的元组元素类型
- 如何从另一个仅静态选择满足特定类型规则的索引的元组实例创建元组实例