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

如何反轉可變參數模板函數的參數順序?

How to reverse the order of arguments of a variadic template function?(如何反轉可變參數模板函數的參數順序?)
本文介紹了如何反轉可變參數模板函數的參數順序?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個帶有 varargs 模板參數模板函數,就像這樣

I have a template function with varargs template arguments, like this

template<typename Args...>
void ascendingPrint(Args... args) { /* ... */ }

我想寫

template<typename Args...>
void descendingPrint(Args... args) {
  /* implementation using ascendingPrint()? */
}

我如何反轉參數包 args 的順序,然后再傳遞它,即在偽代碼中:

How do I reverse the order of the parameter-pack args before passing it along, i.e. in pseudo-code:

template<typename Args...>
void descendingPrint(Args... args) {
  ascendingPrint( reverse(args) );
}

推薦答案

這里是一個遞歸的特殊revert<>實現:

Here is a recursive implementation of a specialized revert<>:

// forward decl
template<class ...Tn>
struct revert;

// recursion anchor
template<>
struct revert<>
{
    template<class ...Un>
    static void apply(Un const&... un)
    {
        ascendingPrint(un...);
    }
};

// recursion
template<class T, class ...Tn>
struct revert<T, Tn...> 
{
    template<class ...Un>
    static void apply(T const& t, Tn const&... tn, Un const&... un)
    {
        // bubble 1st parameter backwards
        revert<Tn...>::apply(tn..., t, un...);
    }
};

// using recursive function
template<class A, class ...An>
void descendingPrint(A const& a, An const&... an)
{
    revert<An...>::apply(an..., a);
}

它適用于 gcc-4.6/7/8clang 并且可能符合標準——唯一困難的部分是調用revert::apply(tn..., t, un...).

It works with gcc-4.6/7/8 and clang and is probably standard compliant -- the only difficult part being the call of revert<Tn...>::apply(tn..., t, un...).

雖然它有缺點(就像遞歸經常有的那樣),它會生成目標函數的大量模板實例(代碼膨脹)并且不使用完美轉發,這可能是一個問題(但也許可以改進使用它).

It has drawbacks though (as recursion often has), that it generates a lot of template-instantiations of the target function (code bloat) and does not use perfect forwarding, which may be an issue (but maybe could be improved to use it).

這篇關于如何反轉可變參數模板函數的參數順序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 麻豆久久久9性大片 | 成人精品一区亚洲午夜久久久 | 欧美国产日韩精品 | 久久久久久国产 | 国产乱码精品一区二区三区忘忧草 | 欧美日韩久久精品 | 国产精品视频一区二区三 | 午夜三区 | 7777奇米影视 | 观看毛片 | 视频在线一区二区 | 欧美精品久久久久久久久久 | 日本黄色免费视频 | 日韩成人精品视频 | 午夜国产 | 国产91在线播放精品91 | 精品久久精品 | 午夜私人影院 | 91精品国产一区二区在线观看 | 91亚洲精华国产 | 在线毛片网 | 亚洲美女天堂网 | 午夜视频在线免费观看 | 亚洲狠狠 | 美日韩视频| 欧美日韩中文国产一区发布 | 99热播放| 日日天天| 99精品国自产在线观看 | 91资源在线 | 欧美 视频 | 在线2区 | 1级毛片 | 91久色| 亚洲一区二区三区在线 | 一区二区三区电影在线观看 | 国产美女精品视频免费观看 | 国产高清在线 | 亚洲 中文 欧美 日韩 在线观看 | 亚洲精品一区二区三区蜜桃久 | 精产国产伦理一二三区 |