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

成員初始化列表錯誤中的模板基構造函數調用

Template base constructor call in member initialization list error(成員初始化列表錯誤中的模板基構造函數調用)
本文介紹了成員初始化列表錯誤中的模板基構造函數調用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個如下所示的基類:

I have a base class that looks like the following:

template<typename T>
class Base
{
   public:
      Base(int someValue);

      virtual T someFunc() =0;
};

template<typename T>
Base<T>::Base(int someValue)
{}

然后是:

#include "base.hpp"

class Foo
   : public Base<Foo>
{
   public:
      Foo(int someValue);

      virtual Foo someFunc();
};

Foo::Foo(int someValue)
   : Base(someValue)
{}

我從 gcc 4.2.1 得到以下錯誤.

I get the following error from gcc 4.2.1.

錯誤:Foo"類沒有任何名為Base"的字段

error: class ‘Foo’ does not have any field named ‘Base’

我應該提到這在我運行 gcc 4.6.2 的 Fedora 機器上編譯得很好.在我的 os x Lion 機器上編譯時出現此錯誤.

I should mention this compiles fine on my Fedora box which is running gcc 4.6.2. This error occurs when compiling on my os x Lion machine.

編輯

問題似乎是我在調用構造函數時沒有在 Foo 類中指明模板的類型.以下修復了 os x 中的錯誤.

Problem seems to be that I am not indicating type of template in the Foo class when calling the constructor. The following fixes the error in os x.

: Base<Foo>(someValue, parent)

編輯

是的,這確實看起來像一個錯誤.我之前提到的修復了 os x 下的錯誤,并且代碼在 Fedora 中通過該修復可以正常編譯.去看看os x中gcc有沒有更新.

Yes this does look like a bug. What I mentioned before fixes the error under os x and code compiles fine in fedora with that fix. Will go and see if there is an update to gcc in os x.

推薦答案

第一:

[C++11: 12.6.2/3]: mem-initializer-list 可以使用任何 class-or- 初始化基類decltype 表示該基類類型.

[C++11: 12.6.2/3]: A mem-initializer-list can initialize a base class using any class-or-decltype that denotes that base class type.

[ 示例:

struct A { A(); };
typedef A global_A;
struct B { };
struct C: public A, public B { C(); };
C::C(): global_A() { } // mem-initializer for base A

——結束示例 ]

而且 Base 應該是一個有效的 injected-class-name 在這里的基礎(也就是說,你可以用它代替 Base;):

And Base should be a valid injected-class-name for the base here (that is, you can use it in place of Base<T>):

[C++11: 14.6.1/1]: 像普通(非模板)類一樣,類模板有一個注入類名(第 9 條).injected-class-name 可以用作 template-nametype-name. 當它與模板參數列表,作為模板模板參數模板參數,或者作為中的最終標識符詳細類型說明符在友元類模板聲明中,它指的是類模板本身.否則,它等價于 template-name 后跟 template-parameters 包含在 <> 中的類模板的template-parameters.

[C++11: 14.6.1/1]: Like normal (non-template) classes, class templates have an injected-class-name (Clause 9). The injected-class-name can be used as a template-name or a type-name. When it is used with a template-argument-list, as a template-argument for a template template-parameter, or as the final identifier in the elaborated-type-specifier of a friend class template declaration, it refers to the class template itself. Otherwise, it is equivalent to the template-name followed by the template-parameters of the class template enclosed in <>.

[C++11: 14.6.1/3]: 類模板或類模板特化的 injected-class-name 可以用作template-nametype-name 范圍內的任何位置.[ 示例:

[C++11: 14.6.1/3]: The injected-class-name of a class template or class template specialization can be used either as a template-name or a type-name wherever it is in scope. [ Example:

template <class T> struct Base {
   Base* p;
};

template <class T> struct Derived: public Base<T> {
   typename Derived::Base* p; // meaning Derived::Base<T>
};

template<class T, template<class> class U = T::template Base> struct Third { };
Third<Base<int> > t; // OK: default argument uses injected-class-name as a template

——結束示例 ]

我沒有發現任何內容表明這不適用于 ctor-initializer,所以我認為這是一個編譯器錯誤.

I haven't found anything to indicate that this doesn't apply in the ctor-initializer, so I'd say that this is a compiler bug.

我的精簡測試用例 在 GCC 4.1.2 中失敗 和 GCC 4.3.4 但在 GCC 4.5.1 (C++11 模式).它似乎由 GCC bug 189 解決;在 GCC 4.5 發行說明中:

My stripped-down testcase fails in GCC 4.1.2 and GCC 4.3.4 but succeeds in GCC 4.5.1 (C++11 mode). It seems to be resolved by GCC bug 189; in the GCC 4.5 release notes:

G++ 現在實施 DR 176.以前 G++ 不支持使用模板基類的注入類名稱作為類型名稱,以及名稱的查找發現模板的聲明在封閉范圍.現在查找名稱找到注入的類名稱,它可以用作類型或模板,具體取決于名稱后面是否跟有模板參數列表.作為一個此更改的結果,以前接受的某些代碼可能是格式錯誤,因為

G++ now implements DR 176. Previously G++ did not support using the injected-class-name of a template base class as a type name, and lookup of the name found the declaration of the template in the enclosing scope. Now lookup of the name finds the injected-class-name, which can be used either as a type or as a template, depending on whether or not the name is followed by a template argument list. As a result of this change, some code that was previously accepted may be ill-formed because

  • 注入的類名不可訪問,因為它來自私有庫,或者
  • 注入的類名不能用作模板模板參數的參數.

在這兩種情況中的任何一種情況下,都可以通過添加嵌套名稱說明符顯式命名模板.第一個可以使用 -fno-access-control 解決;第二個只被拒絕與-迂腐.

In either of these cases, the code can be fixed by adding a nested-name-specifier to explicitly name the template. The first can be worked around with -fno-access-control; the second is only rejected with -pedantic.

<小時>

我用 Qt 抽象出來的精簡測試用例:


My stripped-down testcase with Qt abstracted out:

template <typename T>
struct Base { };

struct Derived : Base<Derived> { // I love the smell of CRTP in the morning
   Derived();
};

Derived::Derived() : Base() {};

這篇關于成員初始化列表錯誤中的模板基構造函數調用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 塑木弯曲试验机_铜带拉伸强度试验机_拉压力测试台-倾技百科 | 四川职高信息网-初高中、大专、职业技术学校招生信息网 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 钢丝绳探伤仪-钢丝绳检测仪-钢丝绳探伤设备-洛阳泰斯特探伤技术有限公司 | 包塑丝_高铁绑丝_地暖绑丝_涂塑丝_塑料皮铁丝_河北创筹金属丝网制品有限公司 | Win10系统下载_32位/64位系统/专业版/纯净版下载 | 水热合成反应釜-防爆高压消解罐-西安常仪仪器设备有限公司 | 超声波_清洗机_超声波清洗机专业生产厂家-深圳市好顺超声设备有限公司 | 土壤养分检测仪|土壤水分|土壤紧实度测定仪|土壤墒情监测系统-土壤仪器网 | 地图标注-手机导航电子地图如何标注-房地产商场地图标记【DiTuBiaoZhu.net】 | 混合生育酚_醋酸生育酚粉_琥珀酸生育酚-山东新元素生物科技 | 兰州牛肉面加盟,兰州牛肉拉面加盟-京穆兰牛肉面 | ◆大型吹塑加工|吹塑加工|吹塑代加工|吹塑加工厂|吹塑设备|滚塑加工|滚塑代加工-莱力奇塑业有限公司 | 酸度计_PH计_特斯拉计-西安云仪| 软文推广发布平台_新闻稿件自助发布_媒体邀约-澜媒宝 | 右手官网|右手工业设计|外观设计公司|工业设计公司|产品创新设计|医疗产品结构设计|EMC产品结构设计 | 办公室家具公司_办公家具品牌厂家_森拉堡办公家具【官网】 | 不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰]-不锈钢法兰-碳钢法兰-法兰盘生产加工厂家-[鼎捷峰] | 电磁铁_推拉电磁铁_机械手电磁吸盘电磁铁厂家-广州思德隆电子公司 | 传爱自考网_传爱自学考试网 | 复盛空压机配件-空气压缩机-复盛空压机(华北)总代理 | 中图网(原中国图书网):网上书店,尾货特色书店,30万种特价书低至2折! | 曙光腾达官网-天津脚手架租赁-木板架出租-移动门式脚手架租赁「免费搭设」 | 517瓜水果特产网|一个专注特产好物的网站 | 丹佛斯变频器-丹佛斯压力开关-变送器-广州市风华机电设备有限公司 | 示波器高压差分探头-国产电流探头厂家-南京桑润斯电子科技有限公司 | 圣才学习网-考研考证学习平台,提供万种考研考证电子书、题库、视频课程等考试资料 | 没斑啦-专业的祛斑美白嫩肤知识网站-去斑经验分享 | 贵阳用友软件,贵州财务软件,贵阳ERP软件_贵州优智信息技术有限公司 | 全自动面膜机_面膜折叠机价格_面膜灌装机定制_高速折棉机厂家-深圳市益豪科技有限公司 | 协议书_协议合同格式模板范本大全 | 锌合金压铸-铝合金压铸厂-压铸模具-冷挤压-誉格精密压铸 | 洛阳永磁工业大吊扇研发生产-工厂通风降温解决方案提供商-中实洛阳环境科技有限公司 | 加中寰球移民官网-美国移民公司,移民机构,移民中介,移民咨询,投资移民 | 建筑资质代办-建筑资质转让找上海国信启航 | 通辽信息港 - 免费发布房产、招聘、求职、二手、商铺等信息 www.tlxxg.net | 合肥触摸一体机_触摸查询机厂家_合肥拼接屏-安徽迅博智能科技 | 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 博客-悦享汽车品质生活 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | ETFE膜结构_PTFE膜结构_空间钢结构_膜结构_张拉膜_浙江萬豪空间结构集团有限公司 |