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

  • <small id='cT1uq'></small><noframes id='cT1uq'>

        <bdo id='cT1uq'></bdo><ul id='cT1uq'></ul>
    1. <tfoot id='cT1uq'></tfoot>

      <i id='cT1uq'><tr id='cT1uq'><dt id='cT1uq'><q id='cT1uq'><span id='cT1uq'><b id='cT1uq'><form id='cT1uq'><ins id='cT1uq'></ins><ul id='cT1uq'></ul><sub id='cT1uq'></sub></form><legend id='cT1uq'></legend><bdo id='cT1uq'><pre id='cT1uq'><center id='cT1uq'></center></pre></bdo></b><th id='cT1uq'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='cT1uq'><tfoot id='cT1uq'></tfoot><dl id='cT1uq'><fieldset id='cT1uq'></fieldset></dl></div>

      <legend id='cT1uq'><style id='cT1uq'><dir id='cT1uq'><q id='cT1uq'></q></dir></style></legend>

        C++模板參數(shù)類型推斷

        c++ template parameter type inference(C++模板參數(shù)類型推斷)
        • <tfoot id='ARAVJ'></tfoot>
        • <legend id='ARAVJ'><style id='ARAVJ'><dir id='ARAVJ'><q id='ARAVJ'></q></dir></style></legend>
              <i id='ARAVJ'><tr id='ARAVJ'><dt id='ARAVJ'><q id='ARAVJ'><span id='ARAVJ'><b id='ARAVJ'><form id='ARAVJ'><ins id='ARAVJ'></ins><ul id='ARAVJ'></ul><sub id='ARAVJ'></sub></form><legend id='ARAVJ'></legend><bdo id='ARAVJ'><pre id='ARAVJ'><center id='ARAVJ'></center></pre></bdo></b><th id='ARAVJ'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ARAVJ'><tfoot id='ARAVJ'></tfoot><dl id='ARAVJ'><fieldset id='ARAVJ'></fieldset></dl></div>

                <bdo id='ARAVJ'></bdo><ul id='ARAVJ'></ul>
              • <small id='ARAVJ'></small><noframes id='ARAVJ'>

                  <tbody id='ARAVJ'></tbody>

                  本文介紹了C++模板參數(shù)類型推斷的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時(shí)送ChatGPT賬號(hào)..

                  我在 C++ 中有這樣一個(gè)模板

                  I have such a template in C++

                  template<typename T, T* P> struct Ptr {};
                  

                  所以我可以這樣使用它:

                  so I can use it as such:

                  const int i = 0;
                  Ptr<int, &i> ptr;
                  

                  Ptr<decltype(i), &i> ptr;
                  

                  但我不想指定類型 int 或身份 i 兩次,我只想使用

                  But I don't want to specify the type int or identity i twice, I want to use just

                  Ptr<&i> ptr;
                  

                  并讓編譯器自己找出 int 類型部分.

                  and let the compiler figure out the int type part by itself.

                  我如何聲明我的模板來做到這一點(diǎn)?

                  How can I declare my template to do that ?

                  我讀過這個(gè)問題,但答案是使用宏,這不好:模板c++的模板?

                  I've read this question but the answer is using macros, that's not nice: template of template c++?

                  我可以通過沒有宏的模板來做到這一點(diǎn)嗎?我使用的是 Visual C++ 2013.

                  can I do this by just template without macros ? I'm using Visual C++ 2013.

                  推薦答案

                  UPDATE

                  c++17 引入了P0127R2 使用 auto 聲明非類型模板參數(shù)",允許聲明非類型模板使用 auto 作為實(shí)際類型占位符的參數(shù):

                  c++17 introduced "P0127R2 Declaring non-type template parameters with auto", allowing to declare a non-type template parameter(s) with auto as a placeholder for the actual type:

                  template <auto P> struct Ptr {};
                  

                  也就是說,P 是一個(gè)非類型模板參數(shù).它的類型可以通過 decltype(P) 推斷出來.

                  That is, P is a non-type template parameter. Its type can be inferred with decltype(P).

                  auto 遵循眾所周知的推導(dǎo)和偏序規(guī)則.在您的情況下,可以將類型限制為僅接受指針:

                  auto in a template parameter list is subject to well-known deduction and partial ordering rules. In your case, the type can be constrained to accept pointers only:

                  template <auto* P> struct Ptr {};
                  

                  請注意,即使對于更詳細(xì)的檢查,使用 auto 的語法也足夠了,例如:

                  Note that the syntax utilizing auto is sufficient even for more detailed inspection, e.g.:

                  template <typename F>
                  struct FunctionBase;
                  
                  template <typename R, typename... Args>
                  struct FunctionBase<R(*)(Args...)> {};
                  
                  template <auto F>
                  struct Function : FunctionBase<decltype(F)> {};
                  

                  也可以使用推斷類型作為其他模板參數(shù)的約束:

                  It's also possible to use the inferred type as a contraint for other template parameters:

                  template <auto I, decltype(I)... Is>
                  struct List {};
                  

                  <小時(shí)>

                  舊答案

                  既然您問的是一個(gè)沒有宏定義幫助的基于類模板的純解決方案,那么答案很簡單:至于現(xiàn)在(2014 年 12 月,c++14) 不可能.

                  Since you are asking about a pure class template-based solution without the help of macro definitions then the answer is simple: as for now (Dec 2014, c++14) it is not possible.

                  這個(gè)問題已經(jīng)被 WG21 C++ 標(biāo)準(zhǔn)委員會(huì)確定為需要,并且有幾個(gè)建議讓模板自動(dòng)推斷非類型模板參數(shù)的類型.

                  This issue has been already identified by the WG21 C++ Standard Committee as a need and there are several proposals to let templates automatically infer the type of non-type template arguments.

                  最接近的是N3601 隱式模板參數(shù):

                  此示例的目的是消除對冗余 template 習(xí)語的需要.這個(gè)習(xí)語被廣泛使用,在谷歌上的點(diǎn)擊量超過 10 萬次.

                  Implicit template parameters

                  The purpose of this example is to eliminate the need for the redundant template<typename T, T t> idiom. This idiom is widely used, with over 100k hits on Google.

                  目標(biāo)是能夠替換像template這樣的模板聲明.struct C; 和另一個(gè)聲明,這樣我們就可以像 C<&X::f> 一樣實(shí)例化模板,而不必說 C.

                  The goal is to be able to replace a template declaration like template<typename T, T t> struct C; with another declaration so that we can instantatiate the template like C<&X::f> instead of having to say C<decltype(&X::f), &X::f>.

                  基本思想是能夠說template;struct C {/* ... *

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

                  相關(guān)文檔推薦

                  Why do two functions have the same address?(為什么兩個(gè)函數(shù)的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復(fù)制構(gòu)造的?)
                  mixing templates with polymorphism(混合模板與多態(tài)性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時(shí)候應(yīng)該使用關(guān)鍵字“typename?使用模板時(shí))
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標(biāo)準(zhǔn)庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數(shù)模板,而 clang 不能)

                    <i id='k15I8'><tr id='k15I8'><dt id='k15I8'><q id='k15I8'><span id='k15I8'><b id='k15I8'><form id='k15I8'><ins id='k15I8'></ins><ul id='k15I8'></ul><sub id='k15I8'></sub></form><legend id='k15I8'></legend><bdo id='k15I8'><pre id='k15I8'><center id='k15I8'></center></pre></bdo></b><th id='k15I8'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='k15I8'><tfoot id='k15I8'></tfoot><dl id='k15I8'><fieldset id='k15I8'></fieldset></dl></div>
                    <tfoot id='k15I8'></tfoot>
                      <bdo id='k15I8'></bdo><ul id='k15I8'></ul>
                    • <small id='k15I8'></small><noframes id='k15I8'>

                      <legend id='k15I8'><style id='k15I8'><dir id='k15I8'><q id='k15I8'></q></dir></style></legend>
                              <tbody id='k15I8'></tbody>
                            主站蜘蛛池模板: 亚洲自拍偷拍视频 | 国产精品7777777 | 激情五月婷婷综合 | 一区二区三区国产精品 | 久久亚洲国产精品 | 91看片网 | 色综合天天综合网国产成人网 | 国产日韩欧美中文 | 欧美成人精品在线 | 天天搞天天搞 | 久草在线青青草 | 91视频免费在观看 | 欧美成人h版在线观看 | 久久久夜色精品亚洲 | 一区二区三区欧美在线 | 国产成人在线观看免费 | 亚洲网站观看 | 亚洲 中文 欧美 日韩 在线观看 | 国产精品日产欧美久久久久 | 成人福利网站 | 在线亚洲免费视频 | 成人三级av| 久久久久亚洲 | 91麻豆产精品久久久久久夏晴子 | h视频免费在线观看 | 一区二区免费 | 精品久久久网站 | 欧美日韩专区 | 最新免费黄色网址 | 国产精品久久久久久一区二区三区 | 一区二区手机在线 | 亚洲vs天堂 | 福利一区在线观看 | 韩日一区二区 | 免费成人在线网 | 亚洲精品在线视频 | 中文字幕 在线观看 | 91麻豆精品国产91久久久资源速度 | 在线成人免费视频 | 91麻豆精品国产91久久久更新资源速度超快 | 91精品国产乱码久久久久久 |