我已经有一个给出tuple
的模板,而index_sequence
可以创建一个新的tuple
,其中包含由该序列索引的元素。此功能模板称为project
我想如果我能以某种方式计算两个index_sequence
的差异,那么我就完成了:我将project
和make_index_sequence<original_tuple_t>
的差异传递给GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder = gsonBuilder.setPrettyPrinting(); //Sets pretty formatting
Gson gson = gsonBuilder.create(); //Create Gson reference
System.out.println(gson.toJson(object)); //prints in pretty format
分类和独特。
这是一个好方法吗?你知道如何实现这个吗?
答案 0 :(得分:1)
这是一种可能的实施方式。
首先,我们将从一个简单的特征开始,将std::size_t
添加到std::index_sequence
的前面:
template <std::size_t First, typename Seq>
struct sequence_cat;
template <std::size_t First, std::size_t... Seq>
struct sequence_cat <First, std::index_sequence<Seq...>> {
using type = std::index_sequence<First, Seq...>;
};
//helper
template <std::size_t First, typename Seq>
using sequence_cat_t = typename sequence_cat<First, Seq>::type;
现在我们将定义一个名为not_in_sequence
的特征,该特征将递归检查两个std::index_sequence
的前端并过滤掉第一个中出现的任何内容:
//empty first sequence
template <class First, class Second>
struct not_in_sequence {
using type = std::index_sequence<>;
};
//helper
template <class First, class Second>
using not_in_sequence_t = typename not_in_sequence<First, Second>::type;
//filter and recurse
template <std::size_t... First, std::size_t... Second,
std::size_t FirstHead, std::size_t SecondHead>
struct not_in_sequence <std::index_sequence<FirstHead, First...>,
std::index_sequence<SecondHead, Second...>> {
using seq1 = std::index_sequence<First...>;
using seq2 = std::index_sequence<Second...>;
using type =
std::conditional_t<
(FirstHead == SecondHead),
not_in_sequence_t<seq1, seq2>,
sequence_cat_t<
FirstHead,
not_in_sequence_t<seq1, sequence_cat_t<SecondHead, seq2>>
>
>;
};
//empty second sequence
template <std::size_t... First, std::size_t FirstHead>
struct not_in_sequence <std::index_sequence<FirstHead, First...>,
std::index_sequence<>> {
using type = std::index_sequence<FirstHead, First...>;
};
你可以这样使用not_in_sequence_t
:
not_in_sequence_t<std::make_index_sequence<3>, std::index_sequence<0,2>>
//same as std::index_sequence<1>
我可能会遗漏一些边缘情况,但您可以随意编辑。