gpt4 book ai didi

c++ - 在 C++17 中定义可变坐标(元组)类型?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:41:43 25 4
gpt4 key购买 nike

我想定义一个可变元组类型来表示坐标。例如,对于某些魔法类型:

template <unsigned int N>
struct CoordT {
typedef std::tuple<_some_magic_> coord_type;
};

我想要 CoordT<3>::coord_type成为 3 维坐标类型:

std::tuple<double, double, double>

.

但是我不知道如何使用模板编程来生成N重复 double

谁能帮忙解释一下怎么写?

最佳答案

使用 std::make_integer_sequence 生成适当长度的包,然后将元素映射到 double 值:

template <size_t n>
struct TupleOfDoubles {
template <size_t... i>
static auto foo(std::index_sequence<i...>) {
return std::make_tuple(double(i)...);
}
using type = decltype(foo(std::make_index_sequence<n>{}));
};

http://coliru.stacked-crooked.com/a/7950876813128c55

关于c++ - 在 C++17 中定义可变坐标(元组)类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54101555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com