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

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

    2. <tfoot id='Ej4Cv'></tfoot>
      <legend id='Ej4Cv'><style id='Ej4Cv'><dir id='Ej4Cv'><q id='Ej4Cv'></q></dir></style></legend>

      <small id='Ej4Cv'></small><noframes id='Ej4Cv'>

        <bdo id='Ej4Cv'></bdo><ul id='Ej4Cv'></ul>

      在 typedef 和 new 中使用 typename 關鍵字

      Use of typename keyword with typedef and new(在 typedef 和 new 中使用 typename 關鍵字)
      • <i id='y8pWP'><tr id='y8pWP'><dt id='y8pWP'><q id='y8pWP'><span id='y8pWP'><b id='y8pWP'><form id='y8pWP'><ins id='y8pWP'></ins><ul id='y8pWP'></ul><sub id='y8pWP'></sub></form><legend id='y8pWP'></legend><bdo id='y8pWP'><pre id='y8pWP'><center id='y8pWP'></center></pre></bdo></b><th id='y8pWP'></th></span></q></dt></tr></i><div class="aqmoek2" id='y8pWP'><tfoot id='y8pWP'></tfoot><dl id='y8pWP'><fieldset id='y8pWP'></fieldset></dl></div>

        <small id='y8pWP'></small><noframes id='y8pWP'>

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

            <tbody id='y8pWP'></tbody>

            <bdo id='y8pWP'></bdo><ul id='y8pWP'></ul>
            1. <tfoot id='y8pWP'></tfoot>
              • 本文介紹了在 typedef 和 new 中使用 typename 關鍵字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                限時送ChatGPT賬號..

                考慮這個代碼,

                template<class T>
                struct Sample
                { 
                     typename T::X *x; //declare pointer to T's X
                };
                

                在上面的代碼中,關鍵字typename是編譯器所必需的,以便它可以在模板中消除嵌套類型和嵌套值之間的歧義.這意味著,在沒有 typename 關鍵字的情況下,編譯器會將其解釋為 T::X 與 x 的乘法,

                In the above code, the keyword typename is required by the compiler, so that it can disambiguate between nested types and nested values in templates. That means, in the absence of typename keyword, compiler would interpret this as multiplication of T::X with x,

                T::X *x; //multiply T::X with x
                

                因此在可能出現歧義的情況下,關鍵字typename 變成了necessity 以消除歧義.但是,上下文本身消除歧義的情況很少.其他主題討論了基類和函數的上下文-參數(盡管后者不會消除歧義).在這個話題中,我特別想討論另外兩個看起來無歧義的上下文,但我們仍然需要寫typename,

                So in such situations where ambiguity can arise, the keyword typename becomes necessity so as to remove ambiguities. But there are few situations when the context itself removes ambiguities. The other topic discusses contexts of base-class and function-parameters (the latter doesn't remove ambiguity though). In this topic, I particularly want to discuss other two contexts which seem to be unambiguous, but we're still required to write typename,

                typedef typename T::X xtype;
                pX = new typename T::X;  
                

                在這兩種情況下,關鍵字typedefnew 足以讓編譯器清楚地知道接下來是type, 不是 價值.

                In these two situations, the keywords typedef and new make it clear enough to the compiler that whatever follows is type, not value.

                所以我的問題是,為什么編譯器仍然需要 typename 關鍵字,即使在明確的情況下,例如當我們使用 typedefnew 時?

                So my question is, why do compilers still need the typename keyword, even in unambiguous situations such as when we use typedef and new?

                //typedef NOT followed by a type!
                int typedef A;
                

                這種語法要求我稍微修改我的問題,以便其他人可以看到我試圖提出的觀點.

                This syntax asks me to modify my question a little bit, so that the point which I'm trying to make, may be seen by others.

                考慮一下,

                T::X typedef *x;
                

                所以從上下文來看,編譯器仍然很清楚 T::X 是一種類型,無論它出現在 before typedef 還是 typedef 之后.除非C++允許我們寫typedef 5 5typedef T::value t_value(其中T::value是value),typedef 本身的存在消除了所有歧義,因此,typename 似乎是標準不必要的要求(在這種情況下).同樣的論點也適用于 new.

                So from the context, it's still clear enough to the compiler that T::X is a type, no matter whether it appears before typedef,or after typedef. Unless C++ allows us to write typedef 5 five or typedef T::value t_value (where T::value is value), the presence of typedef itself removes all ambiguities and so, typename seems to be an unnecessary requirement by the Standard (in such situations). Same argument holds true for new as well.

                另外,我寫了一個類模板,它使用這個結構作為模板參數:

                Also, I've written a class template which is using this struct as template argument:

                struct A 
                {
                        struct X { string name; };
                        static const int X = 100;
                };
                

                我特別想知道以下代碼(來自構造函數)是否正確(可移植),

                I particularly want to know if the following code (from the constructor) is correct (portable) or not,

                //two interesting statements
                 pX = new typename T::X; //T::X means struct X
                 product = T::X * p; //but here, T::X means int X
                

                完整代碼位于 ideone 此處.回復之前請先看一下.:-)

                The complete code is here at ideone. Please have a look at it before replying. :-)

                推薦答案

                C++ 語法比這更瘋狂.

                C++ syntax is more crazy than that.

                // typedef NOT followed by a type!
                int typedef A;
                
                // new NOT followed by a type!
                new (0) int;
                

                其他人對您的示例發表了評論.typename 說明符不會忽略非類型名稱進行查找.所以如果你說new typename T::X,并且T中有一個對象名X,它仍然會被找到而不是類型名稱 X (然而,GCC 在查找 typename 之后的名稱時會忽略非類型名稱.但這不符合標準).

                Others have commented about your example. The typename specifier does not yield to lookup ignoring non-type names. So if you say new typename T::X, and there is an object name X in T, it will still be found instead of the type name X (GCC however ignores non-type names in looking up a name after a typename. But that's not Standards compliant).

                對編輯的回答:

                考慮一下,

                T::X typedef *x;
                

                所以從上下文來看,無論是出現在 typedef 之前,還是出現在 typedef 之后,編譯器仍然很清楚 T::X 是一種類型.

                So from the context, it's still clear enough to the compiler that T::X is a type, no matter whether it appears before typedef,or after typedef.

                編譯器必須知道聲明說明符和(即類型部分"和聲明符部分何時開始(即名稱"部分).有些聲明類型部分為空:

                The compiler has to know when the declaration specifiers and (i.e the "type section" and when the declarator section start (i.e the "names" section). There are declarations where the type section is empty:

                // constructor definitions don't need a type section
                MyClass::MyClass() { }
                
                // conversion function definitions don't need a type section
                MyClass::operator int() { }
                

                如果您指定的名字不是類型,則類型部分結束,名稱部分開始.說 T::X 告訴編譯器:

                If the first name you specify is not a type, the type section ends, and the name section starts. Saying T::X tells the compiler:

                現在我想定義T::X.

                它從左到右讀取,所以當它遇到typedef時它會認為你忘記了分號.在類內部,解釋略有不同,但也很相似.這是一個簡單而有效的解析.

                It reads from left to right, so it will think you forgot a semicolon when it then encounters the typedef. Inside classes the interpretation is slightly different but much like this too. That's a simple and effective parse.

                同樣的論點也適用于 new.

                Same argument holds true for new as well.

                我傾向于同意你的看法.從語法上講,如果省略括號應該是明確的.由于我從未編寫過 C++ 解析器,因此可能存在我沒有看到的隱藏陷阱.

                I tend to agree with you here. Syntactically it should be unambiguous if you leave off parentheses. As I've never written a C++ parser, there may be hidden pitfalls I'm not seeing, though.

                在諸如 new 這樣的語言的極端情況下,每次添加 typename 都可能需要為編譯器和標準編寫者進行大量的設計,同時仍然需要 typename 用于絕大多數其他需要它的情況.我不認為這是值得的.

                Every addition of typename in corner cases of the language like in new will potentially require substantial amount of design for both compilers and standards writers, while still requiring typename for the vast majority of other cases where it's needed. I don't think that this pays off.

                這篇關于在 typedef 和 new 中使用 typename 關鍵字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                相關文檔推薦

                Why do two functions have the same address?(為什么兩個函數的地址相同?)
                Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                mixing templates with polymorphism(混合模板與多態性)
                When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 clang 不能)
                  <i id='pytM1'><tr id='pytM1'><dt id='pytM1'><q id='pytM1'><span id='pytM1'><b id='pytM1'><form id='pytM1'><ins id='pytM1'></ins><ul id='pytM1'></ul><sub id='pytM1'></sub></form><legend id='pytM1'></legend><bdo id='pytM1'><pre id='pytM1'><center id='pytM1'></center></pre></bdo></b><th id='pytM1'></th></span></q></dt></tr></i><div class="qe24ou2" id='pytM1'><tfoot id='pytM1'></tfoot><dl id='pytM1'><fieldset id='pytM1'></fieldset></dl></div>
                    <tbody id='pytM1'></tbody>

                  <small id='pytM1'></small><noframes id='pytM1'>

                      • <bdo id='pytM1'></bdo><ul id='pytM1'></ul>

                        1. <legend id='pytM1'><style id='pytM1'><dir id='pytM1'><q id='pytM1'></q></dir></style></legend><tfoot id='pytM1'></tfoot>
                          主站蜘蛛池模板: 微学堂-电动能源汽车评测_电动车性能分享网 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 反渗透水处理设备|工业零排放|水厂设备|软化水设备|海南净水设备--海南水处理设备厂家 | 示波器高压差分探头-国产电流探头厂家-南京桑润斯电子科技有限公司 | 粒米特测控技术(上海)有限公司-测功机_减速机测试台_电机测试台 | 北京征地律师,征地拆迁律师,专业拆迁律师,北京拆迁律师,征地纠纷律师,征地诉讼律师,征地拆迁补偿,拆迁律师 - 北京凯诺律师事务所 | 食品质构分析仪-氧化诱导分析仪-瞬态法导热系数仪|热冰百科 | 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 电地暖-电采暖-发热膜-石墨烯电热膜品牌加盟-暖季地暖厂家 | 北京网站建设|北京网站开发|北京网站设计|高端做网站公司 | 杭州火蝠电商_京东代运营_拼多多全托管代运营【天猫代运营】 | 机器视觉检测系统-视觉检测系统-机器视觉系统-ccd检测系统-视觉控制器-视控一体机 -海克易邦 | 热处理炉-退火炉-回火炉设备厂家-丹阳市电炉厂有限公司 | 温州在线网| 铝合金脚手架厂家-专注高空作业平台-深圳腾达安全科技 | 驾驶人在线_专业学车门户网站 | 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 电渗析,废酸回收,双极膜-山东天维膜技术有限公司 | led全彩屏-室内|学校|展厅|p3|户外|会议室|圆柱|p2.5LED显示屏-LED显示屏价格-LED互动地砖屏_蕙宇屏科技 | 播音主持培训-中影人教育播音主持学苑「官网」-中国艺考界的贵族学校 | 杭州用友|用友软件|用友财务软件|用友ERP系统--杭州协友软件官网 | 食药成分检测_调料配方还原_洗涤剂化学成分分析_饲料_百检信息科技有限公司 | 河南15年专业网站建设制作设计,做网站就找郑州启凡网络公司 | 数字展示在线_数字展示行业门户网站 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | 哈尔滨治「失眠/抑郁/焦虑症/精神心理」专科医院排行榜-京科脑康免费咨询 一对一诊疗 | 千淘酒店差旅平台-中国第一家针对TMC行业的酒店资源供应平台 | 淋巴细胞分离液_口腔医疗器材-精欣华医疗器械(无锡)有限公司 | 冷却塔降噪隔音_冷却塔噪声治理_冷却塔噪音处理厂家-广东康明冷却塔降噪厂家 | 单机除尘器 骨架-脉冲除尘器设备生产厂家-润天环保设备 | 脉冲除尘器,除尘器厂家-淄博机械 | 磁力加热搅拌器-多工位|大功率|数显恒温磁力搅拌器-司乐仪器官网 | 宽带办理,电信宽带,移动宽带,联通宽带,电信宽带办理,移动宽带办理,联通宽带办理 | 北京银联移动POS机办理_收银POS机_智能pos机_刷卡机_收银系统_个人POS机-谷骐科技【官网】 | 对辊破碎机-液压双辊式,强力双齿辊,四辊破碎机价格_巩义市金联机械设备生产厂家 | 东莞市天进机械有限公司-钉箱机-粘箱机-糊箱机-打钉机认准东莞天进机械-厂家直供更放心! | 电线电缆厂家|沈阳电缆厂|电线厂|沈阳英联塑力线缆有限公司 | 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 | 嘉兴泰东园林景观工程有限公司_花箱护栏| 心得体会网_心得体会格式范文模板| 闪蒸干燥机-喷雾干燥机-带式干燥机-桨叶干燥机-[常州佳一干燥设备] |