久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

std::make_index_sequence 和 std::index_sequence 的細節

details of std::make_index_sequence and std::index_sequence(std::make_index_sequence 和 std::index_sequence 的細節)
本文介紹了std::make_index_sequence 和 std::index_sequence 的細節的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我很喜歡使用可變參數模板,并開始擺弄這個新功能.我試圖了解 std::index_sequence 的實現細節(用于元組實現).我在那里看到了示例代碼,但我真的想要一步一步地解釋 std::index_sequence 是如何編碼的,以及每個階段有問題的元編程原理.想想真的愚蠢的:)

I'm enjoying ramping up on variadic templates and have started fiddling about with this new feature. I'm trying to get my head around the implementation details of std::index_sequence's (used for tuple implementation). I see sample code around there, but I really want a dumbed down step by step explanation of how an std::index_sequence is coded and the meta programming principal in question for each stage. Think really dumbed down :)

推薦答案

我在那里看到了示例代碼,但我真的想要一步一步地解釋 index_sequence 是如何編碼的,以及每個階段的元編程原理.

I see sample code around there, but I really want a dumbed down step by step explanation of how an index_sequence is coded and the meta programming principal in question for each stage.

你問的東西解釋起來并不簡單......

What you ask isn't exactly trivial to explain...

嗯... std::index_sequence 本身很簡單:定義如下

Well... std::index_sequence itself is very simple: is defined as follows

template<std::size_t... Ints>
using index_sequence = std::integer_sequence<std::size_t, Ints...>;

實質上是一個無符號整數的模板容器.

that, substantially, is a template container for unsigned integer.

棘手的部分是 std::make_index_sequence 的實現.也就是說:棘手的部分是從 std::make_index_sequence 傳遞到 std::index_sequence<0, 1, 2, ..., N-1>.

The tricky part is the implementation of std::make_index_sequence. That is: the tricky part is pass from std::make_index_sequence<N> to std::index_sequence<0, 1, 2, ..., N-1>.

我建議您一個可能的實現(不是一個很好的實現,但簡單(我希望)理解),我將嘗試解釋它是如何工作的.

I propose you a possible implementation (not a great implementation but simple (I hope) to understand) and I'll try to explain how it works.

不完全是從 std::integer_sequence 傳遞過來的標準索引序列,但是修復 std::size_t 類型你可以獲得一個合理的 indexSequence/makeIndexSequence 與以下代碼配對.

Non exactly the standard index sequence, that pass from std::integer_sequence, but fixing the std::size_t type you can get a reasonable indexSequence/makeIndexSequence pair with the following code.

// index sequence only
template <std::size_t ...>
struct indexSequence
 { };

template <std::size_t N, std::size_t ... Next>
struct indexSequenceHelper : public indexSequenceHelper<N-1U, N-1U, Next...>
 { };

template <std::size_t ... Next>
struct indexSequenceHelper<0U, Next ... >
 { using type = indexSequence<Next ... >; };

template <std::size_t N>
using makeIndexSequence = typename indexSequenceHelper<N>::type;

我想理解它是如何工作的一個好方法是跟隨一個實際的例子.

I suppose that a good way to understand how it works is follows a practical example.

我們可以點對點地看到 makeIndexSequence<3> 如何變成 index_sequenxe<0, 1, 2>.

We can see, point to point, how makeIndexSequence<3> become index_sequenxe<0, 1, 2>.

  • 我們將 makeIndexSequence<3> 定義為 typename indexSequenceHelper<3>::type [N>3]

  • We have that makeIndexSequence<3> is defined as typename indexSequenceHelper<3>::type [N is 3]

indexSequenceHelper<3> 僅匹配一般情況,因此繼承自 indexSequenceHelper<2, 2> [N3Next... 為空]

indexSequenceHelper<3> match only the general case so inherit from indexSequenceHelper<2, 2> [N is 3 and Next... is empty]

indexSequenceHelper<2, 2> 只匹配一般情況,因此繼承自 indexSequenceHelper<1, 1, 2> [N> 是 2Next...2]

indexSequenceHelper<2, 2> match only the general case so inherit from indexSequenceHelper<1, 1, 2> [N is 2 and Next... is 2]

indexSequenceHelper<1, 1, 2> 僅匹配一般情況,因此繼承自 indexSequenceHelper<0, 0, 1, 2> [N1Next...1, 2]

indexSequenceHelper<1, 1, 2> match only the general case so inherit from indexSequenceHelper<0, 0, 1, 2> [N is 1 and Next... is 1, 2]

indexSequenceHelper<0, 0, 1, 2> 匹配這兩種情況(一般為部分特化),因此應用部分特化并定義 type = indexSequence<0, 1, 2> [Next...0, 1, 2]

indexSequenceHelper<0, 0, 1, 2> match both cases (general an partial specialization) so the partial specialization is applied and define type = indexSequence<0, 1, 2> [Next... is 0, 1, 2]

結論:makeIndexSequence<3>indexSequence<0,1,2>.

希望這會有所幫助.

--- 編輯---

一些說明:

  • std::index_sequencestd::make_index_sequence 從 C++14 開始可用

  • std::index_sequence and std::make_index_sequence are available starting from C++14

我的示例很簡單(我希望)易于理解,但是(正如 aschepler 所指出的)具有很大的限制,即線性實現;我的意思是:如果你需要 index_sequence<0, 1, ... 999>,使用 makeIndexSequence<1000> 你可以遞歸地實現 1000 個不同的 indexSequenceHelper;但是有一個遞歸限制(編譯器形式編譯器不同)可以小于 1000;還有其他算法可以限制遞歸次數,但解釋起來更復雜.

my example is simple (I hope) to understand but (as pointed by aschepler) has the great limit that is a linear implementation; I mean: if you need index_sequence<0, 1, ... 999>, using makeIndexSequence<1000> you implement, in a recursive way, 1000 different indexSequenceHelper; but there is a recursion limit (compiler form compiler different) that can be less than 1000; there are other algorithms that limits the number of recursions but are more complicated to explain.

這篇關于std::make_index_sequence 和 std::index_sequence 的細節的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 一区二区三区国产精品 | 在线播放亚洲 | 久久大 | 欧美日韩中文在线 | 亚洲一区不卡 | 人人干在线 | 一区二区三区四区不卡 | 在线欧美视频 | 国产精品视频一区二区三区, | 国产一区二区三区不卡av | 成年人免费网站 | 国产精品久久久久久久久大全 | 久久一区二区视频 | 熟女毛片 | 亚洲综合视频 | 欧美美女一区二区 | 国产高清精品在线 | 自拍视频网 | 欧美精品一二三区 | 国产日韩欧美一区 | 精品久久久久久亚洲精品 | 午夜欧美一区二区三区在线播放 | 中文字幕在线观看视频网站 | 亚洲国产精品视频一区 | 国产a区| 亚洲一区视频在线 | 亚洲国产精品网站 | 久久精品毛片 | 欧美一区二区三区视频 | 国产高清在线观看 | 99久久久久久 | 国产精品一区视频 | 午夜免费| 欧美男人天堂 | 在线播放一区 | 亚洲韩国精品 | 欧美成人激情 | 亚洲国产乱码 | 成人免费视频一区二区 | 久久精品一区二区 | 综合国产 |