将可变迭代器参数转换为值类型的元组
convert variadic iterator arguments to tuple of value types
如何将IteratorTypes
的模板参数包转换为相应value_type
的元组?我尝试了以下操作,但失败了
error: wrong number of template arguments (2, should be 1)
using ValueTypes = typename std::iterator_traits<IteratorTypes...>::value_type;
^
#include <type_traits>
#include <vector>
#include <tuple>
template <typename ... IteratorTypes>
void foo(IteratorTypes&&...its)
{
using ValueTypes = typename std::iterator_traits<IteratorTypes...>::value_type;
static_assert(std::is_same<std::tuple<ValueTypes>,std::tuple<int,float>>::value, "types do no match");
}
int main() {
std::vector<int> int_vec(1);
std::vector<float> float_vec(1);
foo(int_vec.begin(), float_vec.begin());
return 0;
}
在coliru 上直播
...
紧跟在要展开的模式之后。
using ValueTypes = std::tuple<typename std::iterator_traits<IteratorTypes>::value_type...>;
static_assert(std::is_same<ValueTypes, std::tuple<int,float>>::value, "types do no match");
此外,在将其传递给iterator_traits
之前,您可能应该先std::decay
IteratorTypes
,或者按值而不是通过转发引用获取它们。
。。。或者为了更容易阅读:
#include <type_traits>
#include <vector>
#include <tuple>
#include <utility>
template <typename ... IteratorTypes>
void foo(IteratorTypes&&...its)
{
using value_type_tuple = std::tuple<typename IteratorTypes::value_type...>;
using desired_tuple = std::tuple<int, float>;
static_assert(std::is_same<value_type_tuple , desired_tuple>::value, "types do no match");
}
int main() {
std::vector<int> int_vec(1);
std::vector<float> float_vec(1);
foo(int_vec.begin(), float_vec.begin());
return 0;
}
相关文章:
- 在C++中,如何通过几种类型从元组中选择多个元素
- 将元组类型扩展为可变参数模板?
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- 创建派生自可变参数模板包的类型元组
- 枚举元组类型的最佳方法?
- 我无法在用forward_as_tuple创建的元组中按类型访问元素
- Boost.Hana:在 constexpr 上下文中将值元组转换为相应类型的元组
- 在C++中返回基元类型数组(Java比较)
- 将函数应用于元组中的每个元素,将每个元素强制转换为类型包中的不同类型,然后作为参数包传递
- 如何将值元组中的相应值附加到相同类型的向量元组中
- 您自己的类型的结构化绑定,不是结构或元组(通过公共成员函数)
- 如何在编译器时动态获取元组内的变量类型C++?
- 验证(使用 static_assert)元组类型是否遵循某种顺序(有状态编译时检查)
- 使用每种类型的可变参数模板上的类模板初始化元组
- 在 c++17 中筛选类型元组
- 有没有办法将一对元组剥离为可变参数模板类型或实例化具有可变参数类型的东西?
- 从类型元组到通过调用模板函数返回的值数组
- 为什么不采用非类型部分专用元组?
- 以后可以解压缩类型元组吗?
- 如何根据表达式在Boost.Hana中是否有效来过滤类型元组