[英]defining a variadic coordinate (tuple) type in C++17?
I wanted to define a variadic tuple type to represent coordinates. 我想定义一个可变元组的类型来表示坐标。 For example, for some magic type:
例如,对于某些魔术类型:
template <unsigned int N>
struct CoordT {
typedef std::tuple<_some_magic_> coord_type;
};
I'd like to have CoordT<3>::coord_type
to be the 3-dimensional coordinate type: 我想让
CoordT<3>::coord_type
成为三维坐标类型:
std::tuple<double, double, double>
. 。
But I don't know how to use template programming to generate N
repeated double
s. 但我不知道如何使用模板编程来生成
N
重复的double
s。
Can anyone please help explain how to write it? 任何人都可以帮忙解释如何写它?
Use std::make_integer_sequence
to generate a pack of the appropriate length, then map the elements to doubles: 使用
std::make_integer_sequence
生成一个适当长度的包,然后将元素映射到双精度:
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 http://coliru.stacked-crooked.com/a/7950876813128c55
If you don't literally need a std::tuple
, but just need something which acts like a tuple, use std::array
: 如果你不需要
std::tuple
,但只需要像元组一样的东西,使用std::array
:
template <unsigned int N>
struct CoordT {
typedef std::array<double, N> coord_type;
};
std::array
has overloads for std::get<I>
, std::tuple_size
, and std::tuple_element
. std::array
具有std::get<I>
, std::tuple_size
和std::tuple_element
。 Most library and language facilities which accept a tuple-like element will support std::array
, such as std::apply
and structured bindings . 接受类似元组的元素的大多数库和语言工具都将支持
std::array
,例如std::apply
和结构化绑定 。
Is too late to play? 玩得太晚了?
If for you is acceptable to declare (no definition is needed) a variadic template function as follows 如果您可以接受声明(不需要定义)可变参数模板函数,如下所示
template <std::size_t ... Is>
constexpr auto toIndexSeq (std::index_sequence<Is...> a)
-> decltype(a);
and that the coord_type
is defined in a CoordT
specialization, you can write it as follows 并且
coord_type
是在CoordT
专业化中定义的,您可以按如下方式编写它
template <std::size_t N,
typename = decltype(toIndexSeq(std::make_index_sequence<N>{}))>
struct CoordT;
template <std::size_t N, std::size_t ... Is>
struct CoordT<N, std::index_sequence<Is...>>
{ using coord_type = std::tuple<decltype((void)Is, 0.0)...>; };
The following is a full C++14 compiling example 以下是完整的C ++ 14编译示例
#include <tuple>
#include <type_traits>
template <std::size_t ... Is>
constexpr auto toIndexSeq (std::index_sequence<Is...> a)
-> decltype(a);
template <std::size_t N,
typename = decltype(toIndexSeq(std::make_index_sequence<N>{}))>
struct CoordT;
template <std::size_t N, std::size_t ... Is>
struct CoordT<N, std::index_sequence<Is...>>
{ using coord_type = std::tuple<decltype((void)Is, 0.0)...>; };
int main()
{
using t0 = std::tuple<double, double, double, double>;
using t1 = typename CoordT<4u>::coord_type;
static_assert( std::is_same<t0, t1>::value, "!" );
}
A very concise way to do this is by using std::tuple_cat
and std::array
: 一个非常简洁的方法是使用
std::tuple_cat
和std::array
:
template <unsigned int N>
struct CoordT {
using coord_type = decltype(std::tuple_cat(std::array<double, N>{}));
};
std::tuple_cat
is allowed to support tuple-like types such as std::array
, but not guaranteed. 允许
std::tuple_cat
支持类似于元组的类型,例如std::array
,但不能保证。 However, every implementation I checked supports this . 但是,我检查的每个实现都支持这一点 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.