迭代模板int
Iterate over template int
我有一个函数:
template<class Real, int N>
constexpr std::array<Real, N> get_array();
我想对许多类型和许多整数进行测试。在伪代码:
auto types = {float, double, long double};
for(int i = 0; i < 25; ++i) {
for (type : types) {
auto arr = get_array<type, i>();
// test arr
}
}
显然这不会编译。有没有办法修补循环以制作它,所以我可以在数组上迭代?
,由于您有boost.hana标记,我们可以使用它:
auto types = hana::tuple_t<float, double, long double>;
hana::for_each(types, [](auto type){
hana::for_each(std::make_index_sequence<25>(), [=](auto idx){
// here type is an object that denotes the type and
// idx is an integral constant that denotes the next value
get_array<typename decltype(type)::type, idx>();
});
});
接受的答案很好,但是如果您不想污染您的呼叫堆栈,最好在完成实际运行之前完成所有处理 - 时间的东西。
此处的示例是使用hana::cartesian_product
,这可能是临时用例的过度杀伤,但是您可以使用嵌套的hana::unpack
s获得相同的效果。
这是一个有效的示例:
https://godbolt.org/z/zkwpqe
#include <array>
#include <boost/hana/assert.hpp>
#include <boost/hana/at.hpp>
#include <boost/hana/cartesian_product.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/for_each.hpp>
#include <boost/hana/range.hpp>
#include <boost/hana/transform.hpp>
#include <boost/hana/tuple.hpp>
#include <boost/hana/type.hpp>
namespace hana = boost::hana;
template <typename Real, int N>
constexpr std::array<Real, N> get_array() {
return {};
}
int main() {
auto types = hana::tuple_t<float, double, long, double>;
auto lengths = hana::to_tuple(hana::range_c<int, 0, 4>);
auto tls = hana::cartesian_product(hana::make_tuple(types, lengths));
auto get_array_fns = hana::transform(tls, [](auto pair) {
return [] {
return get_array<typename decltype(+hana::at_c<0>(pair))::type,
decltype(+hana::at_c<1>(pair))::value>();
};
});
hana::for_each(get_array_fns, [](auto get_array) {
auto arr = get_array();
// test arr
});
auto result_types = hana::unpack(get_array_fns, [](auto ...get_array) {
return hana::tuple_t<decltype(get_array())...>;
});
BOOST_HANA_CONSTANT_CHECK(hana::equal(
decltype(result_types){},
hana::tuple_t<std::array<float, 0>,
std::array<float, 1>,
std::array<float, 2>,
std::array<float, 3>,
std::array<double, 0>,
std::array<double, 1>,
std::array<double, 2>,
std::array<double, 3>,
std::array<long, 0>,
std::array<long, 1>,
std::array<long, 2>,
std::array<long, 3>,
std::array<double, 0>,
std::array<double, 1>,
std::array<double, 2>,
std::array<double, 3>>));
}
如下(不提升)吗?
#include <array>
#include <iostream>
template <typename T, std::size_t Dim>
constexpr std::array<T, Dim> get_array ()
{ return {}; }
// fake test
template <typename T, std::size_t Dim>
constexpr bool checkArray (std::array<T, Dim> const &)
{ return true; }
template <typename T, std::size_t ... Is>
constexpr bool checkSequence (std::index_sequence<Is...> const &)
{ return (... && checkArray(get_array<T, Is>())); }
template <typename ... Ts>
constexpr bool checkTypes ()
{ return (... && checkSequence<Ts>(std::make_index_sequence<25u>{})); }
int main ()
{
constexpr auto value = checkTypes<float, double, long double>();
std::cout << value << std::endl;
}
相关文章:
- 如何从map<pair<string,int>,pair<string,Array>>中迭代和查找?
- C++:向量<向量<int>>迭代器?
- 如何使用set<pair<int,int> >::iterator itrator it迭代set<pair<int,int> >st中的值?
- map<int,int>的*迭代器是什么?它不是pair<int,int>
- 优先级队列(使用 pairs<int,int> ) 根据 for 循环迭代器的顶级元素
- 从 for(int &i : i) 获取当前迭代器
- 如何在 CPP 中使用 int 迭代器打印数组/列表的向量?
- 当大小未知时,如何迭代指向数组 (int*) 的指针?
- 迭代从 SWIG 绑定返回的 python 中的 vector<pair<int,int>>
- 为什么如果(!(cin >> int)) 在第一次迭代中接受十进制数,但不接受其他十进制数?
- 迭代模板int
- 如何在C++中迭代集合映射(std::map<std::set< char>, int >)?
- 调用"替换(std::basic_string<char>::迭代器, std::basic_string::<char>迭代器, char, int)"没有匹配函数|
- 使用set<set>迭代器的编译错误<int>
- 如何使用迭代器遍历表单 pair<int,pair<int,int>> 的映射
- 如何计算标准::矢量::<int>迭代器和标准::矢量<int>::reverse_iterator之间的距离?
- 从数组列表中的字符串迭代器获取 int dat<struct>
- C++ 通用 int 数组和矢量迭代器
- Vector::<int>迭代器与 list::<int>itrator 键在 std::map
- 迭代器在std::unordered_map<int,std::vector<Element>>