日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

奇异递归模板模式(Curiously Recurring Template Pattern,CRTP)

發布時間:2023/12/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 奇异递归模板模式(Curiously Recurring Template Pattern,CRTP) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

The Curiously Recurring Template Pattern (CRTP) is a C++ idiom whose name was coined by James Coplien in 1995, in early C++ template code.

The “C” in CRTP made it travel the years in the C++ community by being this: a Curiosity. We often find definitions of what CRTP is, and it is indeed an intriguing construct.

But what’s even more interesting is what CRTP means in code, as in what you can express and achieve by using it, and this is the point of this series.

If you have used CRTP in your own code then you certainly know what it is useful for. In this case you may know most of what’s written in this series of posts (though you may want to give a quick glance to episode #2, just to make sure we’re in line), and you can just skip over to episode #3 where I propose a generic helper for CRTP, that I found helpful when using it in code.

The CRTP, episode One: Definition
The CRTP, episode Two: What it can bring to your code
The CRTP, episode Three: Implementation

What CRTP is

The CRTP consists in:

  • inheriting from a template class,
  • use the derived class itself as a template parameter of the base class.

This is what it looks like in code:

template <typename T> class Base {... };class Derived : public Base<Derived> {... };

The purpose of doing this is using the derived class in the base class. From the perspective of the base object, the derived object is itself, but downcasted. Therefore the base class can access the derived class by static_casting itslef into the derived class.

template <typename T> class Base { public:void doSomething(){T& derived = static_cast<T&>(*this);//use derived...} };

Note that contrary to typical casts to derived class, we don’t use dynamic_cast here. A dynamic_cast is used when you want to make sure at run-time that the derived class you are casting into is the correct one. But here we don’t need this guarantee: the Base class is designed to be inherited from by its template parameter, and by nothing else. Therefore it takes this as an assumption, and a static_cast is enough.

What could go wrong

If two classes happen to derive from the same CRTP base class we likely get to undefined behaviour when the CRTP will try to use the wrong class:

class Derived1 : public Base<Derived1> {... };class Derived2 : public Base<Derived1> // bug in this line of code {... };

There is a solution to prevent this, that has been proposed by Marek Kurdej in the comments section. It consists in adding a private constructor in the base class, and making the base class friend with the templated class:

template <typename T> class Base { public:// ... private:Base(){};friend T; };

Indeed, the constructors of the derived class have to call the constructor of the base class (even if you don’t write it explicitly in the code, the compiler will do his best to do it for you). Since the constructor in the base class is private, no one can access it except the friend classes. And the only friend class is… the template class! So if the derived class is different from the template class, the code doesn’t compile. Neat, right?

Another risk with CRTP is that methods in the derived class will hide methods from the base class with the same name. As explained in Effective C++ Item 33, the reason for that is that these methods are not virtual. Therefore you want to be careful not to have identical names in the base and derived classes:

class Derived : public Base<Derived> { public:void doSomething(); // oops this hides the doSomething methods from the base class ! }

The first time I was shown CRTP my initial reaction was: “wait, I didn’t get it”. Then I saw it a couple of other times and I got it. So if you didn’t get how it works, just re-read section 1 a couple of times, and that should do it (if it doesn’t just get in touch and I’ll be happy to talk with you about it).

To tell you the truth, I started by writing a huge blog post about CRTP, which would have been daunting to read completely I think. For that reason I decided to split it up into several logical parts, which constitute the episodes of this series. This post was relatively short, but was necessary to put the basics in place.

Next up: how the CRTP can be useful to your code.

總結

以上是生活随笔為你收集整理的奇异递归模板模式(Curiously Recurring Template Pattern,CRTP)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩美女视频一区 | 国产东北真实交换多p免视频 | 精品黑人一区二区三区国语馆 | 午夜毛片视频 | av一区二区在线播放 | 60分钟| 久久精品国产77777蜜臀 | 欧美一区二区福利 | 欧美性xxxx | 午夜看片网站 | 粉嫩aⅴ一区二区三区四区五区 | 亚洲区中文字幕 | 亚洲av区无码字幕中文色 | 黄色片在线免费观看视频 | 午夜精品久久久久久久久久久久久蜜桃 | 国产丰满美女做爰 | 国语对白一区二区 | 亚洲国产成人精品久久久 | 69亚洲乱人伦 | 色呦呦精品 | 我要看免费的毛片 | 欧美最猛性xxxx | 国产免费一区二区三区免费视频 | 国产自在线拍 | 玖玖玖国产精品 | 尤物天堂 | 毛片av免费看 | 一级黄色在线播放 | av网址导航| 欧美肉大捧一进一出免费视频 | 日韩精品一区二区三区视频 | 天堂中文资源在线观看 | 亚洲天堂日本 | 亲吻刺激视频 | 一区二区三区四区五区av | 黄色一级片在线免费观看 | 波多野结衣伦理 | 18p在线观看 | 女儿的朋友5中汉字晋通话 欧美成人免费高清视频 | 91精品国产色综合久久不卡98口 | 在线毛片网站 | 曰本三级日本三级日本三级 | 天天视频黄色 | 欧美成人三级在线 | 久久韩日 | 久久888 | 日本美女视频 | 激情拍拍 | 最新国产露脸在线观看 | 无码免费一区二区三区免费播放 | 91精品国产综合久久香蕉922 | 欧美激情视频一区二区三区 | 黑人操中国女人视频 | 精品一区在线播放 | 久草a在线 | 四虎影院免费视频 | 波多野结衣视频免费 | 亚洲国产精一区二区三区性色 | 欧美群妇大交群 | 美女撒尿无遮挡网站 | 成av人在线观看 | 黄色av电影网址 | 丁香花电影高清在线阅读免费 | 中国一及毛片 | 国产成人精品a视频一区 | av天天操 | 国产乱码久久久 | 97在线播放免费观看 | 黄网站免费大全入口 | 国产真实乱在线更新 | 99久久人妻无码中文字幕系列 | 夜夜夜操操操 | 琪琪色18 | 午夜国产一区二区三区 | jzzijzzij亚洲成熟少妇18 欧美www在线观看 | 国产成人综合在线 | 亚洲av无码精品色午夜果冻不卡 | 一本久草| 亚洲av无码一区二区三区在线播放 | 青青艹在线视频 | 91久久久久久久久久 | 黄网视频在线观看 | 婷婷一区二区三区四区 | 国产特级aaaaaa大片 | 色欲AV无码精品一区二区久久 | 小珊的性放荡羞辱日记 | 亚洲色图一区二区三区 | 黄视频网站在线观看 | 少妇熟女视频一区二区三区 | 中文字幕第一页av | 五月天婷婷在线观看 | 男生操女生网站 | 成人黄网免费观看视频 | 日韩av网站在线播放 | 日本一区二区久久 | 欧美精品亚洲一区 | 性——交——性——乱免费的 | 国产中文字幕乱人伦在线观看 | 综合久久婷婷 |