pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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="3hj7jdv" 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="rbrt7p7" 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ù)類型推斷的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我在 C++ 中有這樣一個模板

                  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 ?

                  我讀過這個問題,但答案是使用宏,這不好:模板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ù):

                  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 是一個非類型模板參數(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 {};
                  

                  <小時>

                  舊答案

                  既然您問的是一個沒有宏定義幫助的基于類模板的純解決方案,那么答案很簡單:至于現(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.

                  這個問題已經(jīng)被 WG21 C++ 標(biāo)準(zhǔn)委員會確定為需要,并且有幾個建議讓模板自動推斷非類型模板參數(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í)語的需要.這個習(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; 和另一個聲明,這樣我們就可以像 C<&X::f> 一樣實例化模板,而不必說 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?(為什么兩個函數(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(我什么時候應(yīng)該使用關(guān)鍵字“typename?使用模板時)
                  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="597frnb" 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>
                            主站蜘蛛池模板: 烟台条码打印机_烟台条码扫描器_烟台碳带_烟台数据采集终端_烟台斑马打印机-金鹏电子-金鹏电子 | 北京印刷厂_北京印刷_北京印刷公司_北京印刷厂家_北京东爵盛世印刷有限公司 | 民用音响-拉杆音响-家用音响-ktv专用音响-万昌科技 | 建筑工程资质合作-工程资质加盟分公司-建筑资质加盟 | 储气罐,真空罐,缓冲罐,隔膜气压罐厂家批发价格,空压机储气罐规格型号-上海申容压力容器集团有限公司 | 农业四情_农业气象站_田间小型气象站_智慧农业气象站-山东风途物联网 | 西门子伺服电机维修,西门子电源模块维修,西门子驱动模块维修-上海渠利 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 水厂污泥地磅|污泥处理地磅厂家|地磅无人值守称重系统升级改造|地磅自动称重系统维修-河南成辉电子科技有限公司 | 西安文都考研官网_西安考研辅导班_考研培训机构_西安在职考研培训 | RTO换向阀_VOC高温阀门_加热炉切断阀_双偏心软密封蝶阀_煤气蝶阀_提升阀-湖北霍科德阀门有限公司 | 扬尘在线监测系统_工地噪声扬尘检测仪_扬尘监测系统_贝塔射线扬尘监测设备「风途物联网科技」 | 合肥通道闸-安徽车牌识别-人脸识别系统厂家-安徽熵控智能技术有限公司 | 东莞韩创-专业绝缘骨架|马达塑胶零件|塑胶电机配件|塑封电机骨架厂家 | 贴片电容-贴片电阻-二三极管-国巨|三星|风华贴片电容代理商-深圳伟哲电子 | 渗透仪-直剪仪-三轴仪|苏州昱创百科 | ICP备案查询_APP备案查询_小程序备案查询 - 备案巴巴 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 识禅_对禅的了解,从这里开始 | 家用净水器代理批发加盟_净水机招商代理_全屋净水器定制品牌_【劳伦斯官网】 | 硬度计,金相磨抛机_厂家-莱州华煜众信试验仪器有限公司 | 广东风淋室_广东风淋室厂家_广东风淋室价格_广州开源_传递窗_FFU-广州开源净化科技有限公司 | 森旺-A级防火板_石英纤维板_不燃抗菌板装饰板_医疗板 | 厂房出租-厂房规划-食品技术-厂房设计-厂房装修-建筑施工-设备供应-设备求购-龙爪豆食品行业平台 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | led全彩屏-室内|学校|展厅|p3|户外|会议室|圆柱|p2.5LED显示屏-LED显示屏价格-LED互动地砖屏_蕙宇屏科技 | 防腐木批发价格_深圳_惠州_东莞防腐木厂家_森源(深圳)防腐木有限公司 | 电缆故障测试仪_电缆故障定位仪_探测仪_检测仪器_陕西意联电气厂家 | U拓留学雅思一站式服务中心_留学申请_雅思托福培训 | 南京雕塑制作厂家-不锈钢雕塑制作-玻璃钢雕塑制作-先登雕塑厂 | YJLV22铝芯铠装电缆-MYPTJ矿用高压橡套电缆-天津市电缆总厂 | 绿叶|绿叶投资|健康产业_绿叶投资集团有限公司 | (中山|佛山|江门)环氧地坪漆,停车场地板漆,车库地板漆,聚氨酯地板漆-中山永旺地坪漆厂家 | 河北码上网络科技|邯郸小程序开发|邯郸微信开发|邯郸网站建设 | 选矿设备,选矿生产线,选矿工艺,选矿技术-昆明昆重矿山机械 | 英国雷迪地下管线探测仪-雷迪RD8100管线仪-多功能数字听漏仪-北京迪瑞进创科技有限公司 | 福建珂朗雅装饰材料有限公司「官方网站」 | 广州番禺搬家公司_天河黄埔搬家公司_企业工厂搬迁_日式搬家_广州搬家公司_厚道搬迁搬家公司 | 长沙广告公司_制作,长沙喷绘_发光字_招牌制作_长沙泓润广告官网 长城人品牌官网 | 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | 水热合成反应釜-防爆高压消解罐-西安常仪仪器设备有限公司 |