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

C++ 模板中的名稱查找

Name lookups in C++ templates(C++ 模板中的名稱查找)
本文介紹了C++ 模板中的名稱查找的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一些 C++ 代碼在沒有 -fpermissive 選項的情況下不再編譯.這是我無法共享的專有代碼,但我認為我已經能夠提取一個簡單的測試用例來演示問題.這是 g++ 的輸出

I have some C++ code that is no longer compiling without the -fpermissive option. It's propriety code that I can't share, but I've think I've been able to extract a simple test case that demonstrates the problem. Here is the output from g++

template_eg.cpp: In instantiation of 'void Special_List<T>::do_other_stuff(T*) [with T = int]':
template_eg.cpp:27:35:   required from here
template_eg.cpp:18:25: error: 'next' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
template_eg.cpp:18:25: note: declarations in dependent base 'List<int>' are not found by unqualified lookup
template_eg.cpp:18:25: note: use 'this->next' instead

這里是產生問題的代碼:

So here is the code the generates the problem:

template<class T> class List  
{
        public: 
        void next(T*){
            cout<<"Doing some stuff"<<endl;
        }       
};

template<class T> class Special_List: public List<T>
{
    public:
        void do_other_stuff(T* item){
                next(item);
        }       
};


int main(int argc, char *argv[])
{
    Special_List<int> b;
    int test_int = 3;
    b.do_other_stuff(&test_int);
}

我不是想找出如何修復代碼以使其再次編譯.這只是將 next(item) 更改為 this->next(item) 的問題我試圖更好地理解為什么這種改變是必要的.我在這個頁面上找到了一個解釋:http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html雖然這個解釋很有用,但我仍然有一些問題.我的函數采用 T*(指向類型 T 的指針)這一事實不應該使它依賴于模板參數.用我自己的話來說,編譯器(gcc 4.7)不應該能夠確定 next() 函數在基類 List 中嗎?為什么有必要在每個這樣的調用前加上 this-> ?我注意到 clang 3.1 表現出相同的行為,所以我假設 c++ 標準中有一些要求需要這種行為.任何人都可以提供理由嗎?

I am not trying to find out how to fix the code to make it compile again. That's simply a matter of changing next(item) to this->next(item) I'm trying to better understand why this change is necessary. I found an explanation on this page: http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html While that explanation was useful, I still have some questions. Shouldn't the fact that my function takes a T* (pointer to type T) make it dependent on the template argument. In my own wording, shouldn't the compiler (gcc 4.7) be able to figure out that the next() function is in the base class List? Why is it necessary to prepend this-> in front of every such call? I've notice that clang 3.1 exhibits the same behavior, so I assume that there is some requirement in the c++ standard that requires this behavior. Could anyone provide a justification for it?

推薦答案

問題是模板是分兩遍處理的(按照標準,VS 不然).在第一遍中,在類型替換之前,查找并檢查所有不依賴于模板參數的內容.一旦類型被替換,從屬名稱就會在第二次傳遞中被解析.

The problem is that templates are processed in two passes (according to the standard, VS does otherwise). In the first pass, before the type substitution, everything that does not depend on the template arguments is looked up and checked. Dependent names are then left to resolve in the second pass, once the type has been substituted.

現在,在第一遍中沒有任何內容表明next 依賴于模板參數,因此它需要在類型替換之前解析.現在,因為基本類型是在當前模板的模板參數上模板化的,編譯器無法查看它(它可能專門用于某些類型,并且不知道我們正在實例化模板的類型 T與,我們不知道使用哪個專業化,即基礎 dependsT 上,我們在知道 T 之前正在檢查).

Now, in the first pass there is nothing that indicates that next is dependent on template arguments, and thus it needs to resolve before type substitution. Now, because the base type is templated on the template argument of your current template, the compiler cannot look into it (it might be specialized for some types, and without knowing what type T we are instantiating the template with, we cannot know which specialization to use, i.e. the base depends on T and we are checking before knowing T).

添加 this-> 的技巧將 next 變成了依賴名稱,這反過來意味著查找被延遲到第二遍,其中 T 是已知的,因為 T 是已知的,所以 List 也是已知的,可以查詢.

The trick of adding this-> turns next into a dependent name, and that in turn means that lookup is delayed until the second pass, where T is known, and because T is known, List<T> is also known and can be looked up into.

EDIT:上述答案的措辭中缺少的一個重要細節是第二階段查找(在類型替換之后)只會添加在參數相關查找期間找到的函數.也就是說,如果 next 是與 T 關聯的命名空間中的自由函數,它將被找到,但它是基礎上的成員,對于 ADL 上的 ADL 不可見T.

EDIT: One important detail missing in the wording of the answer above is that second phase lookup (after type substitution) will only add functions found during argument dependent lookup. That is, if next was a free function in a namespace associated with T it would be found, but it is a member on the base, which is not visible for ADL on T.

這篇關于C++ 模板中的名稱查找的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 运动木地板厂家_体育木地板安装_篮球木地板选购_实木运动地板价格 | 馋嘴餐饮网_餐饮加盟店火爆好项目_餐饮连锁品牌加盟指南创业平台 | 长信科技产业园官网_西安厂房_陕西标准工业厂房 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 伺服电机_直流伺服_交流伺服_DD马达_拓达官方网站 | 粉末包装机-给袋式包装机-全自动包装机-颗粒-液体-食品-酱腌菜包装机生产线【润立机械】 | 注塑模具_塑料模具_塑胶模具_范仕达【官网】_东莞模具设计与制造加工厂家 | 杭州火蝠电商_京东代运营_拼多多全托管代运营【天猫代运营】 | 防勒索软件_数据防泄密_Trellix(原McAfee)核心代理商_Trellix(原Fireeye)售后-广州文智信息科技有限公司 | 液压扳手-高品质液压扳手供应商 - 液压扳手, 液压扳手供应商, 德国进口液压拉马 | 光环国际-新三板公司_股票代码:838504 | 潍坊大集网-潍坊信息港-潍坊信息网 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 青岛侦探_青岛侦探事务所_青岛劝退小三_青岛调查出轨取证公司_青岛婚外情取证-青岛探真调查事务所 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | TPM咨询,精益生产管理,5S,6S现场管理培训_华谋咨询公司 | 不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰]-不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰] | [官网]叛逆孩子管教_戒网瘾学校_全封闭问题青少年素质教育_新起点青少年特训学校 | 招商帮-一站式网络营销服务|搜索营销推广|信息流推广|短视视频营销推广|互联网整合营销|网络推广代运营|招商帮企业招商好帮手 | 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 | 轴流风机-鼓风机-离心风机-散热风扇-罩极电机,生产厂家-首肯电子 | 富森高压水枪-柴油驱动-养殖场高压清洗机-山东龙腾环保科技有限公司 | 换链神器官网-友情链接交换、购买交易于一体的站长平台 | 玻纤土工格栅_钢塑格栅_PP焊接_单双向塑料土工格栅_复合防裂布厂家_山东大庚工程材料科技有限公司 | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | 上海办公室装修_上海店铺装修公司_厂房装潢设计_办公室装修 | 包装设计公司,产品包装设计|包装制作,包装盒定制厂家-汇包装【官方网站】 | 深圳富泰鑫五金_五金冲压件加工_五金配件加工_精密零件加工厂 | 风淋室生产厂家报价_传递窗|送风口|臭氧机|FFU-山东盛之源净化设备 | 电梯装饰-北京万达中意电梯装饰有限公司| 手持气象站_便携式气象站_农业气象站_负氧离子监测站-山东万象环境 | 面粉仓_储酒罐_不锈钢储酒罐厂家-泰安鑫佳机械制造有限公司 | 破碎机锤头_合金耐磨锤头_郑州宇耐机械工程技术有限公司 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 桁架楼承板-钢筋桁架楼承板-江苏众力达钢筋楼承板厂 | 【星耀裂变】_企微SCRM_任务宝_视频号分销裂变_企业微信裂变增长_私域流量_裂变营销 | 济南展厅设计施工_数字化展厅策划设计施工公司_山东锐尚文化传播有限公司 | 山东信蓝建设有限公司官网| 广州番禺搬家公司_天河黄埔搬家公司_企业工厂搬迁_日式搬家_广州搬家公司_厚道搬迁搬家公司 | 上海洗地机-洗地机厂家-全自动洗地机-手推式洗地机-上海滢皓洗地机 |