C++模板之CRTP

本文介绍了C++中的CRTP(Curiously Recurring Template Pattern),这是一种特殊的模板使用方式,用于实现静态多态。CRTP允许基类在派生类实例化时获取派生类的类型,从而提升效率。文章详细讲解了CRTP的特点,包括其优点(如节省虚函数查询开销)和缺点(降低代码可读性)。同时,给出了CRTP的示例和在enable_shared_from_this中的应用,帮助读者理解如何在实践中运用CRTP。

低功耗蓝牙项目,需要一块懂省电的板

思澈 SF32LB52 芯片,BLE 协议栈深度优化,上手即开发

1. 什么是CRTP

CRTP的全称是Curiously Recurring Template Pattern,即奇异递归模板模式,简称CRTP。CRTP是一种特殊的模板技术和使用方式,是C++模板编程中的一种惯用法。

CRTP的特性表现为:基类是一个模板类;派生类继承该基类时,将派生类自身作为模板参数传递给基类。

// 定义一个模板类作为基类
template <typename T>
class Base
{
    ...
};
 // 定义一个派生类,这个类继承以自身作为参数的基类
class Derived : public Base<Derived>
{
    ...
};

2. CRTP特点

  • 优点:省去动态绑定、查询虚函数表带来的开销。通过CRTP,基类可以获得到派生类的类型,提供各种操作,比普通的继承更加灵活。但CRTP基类并不会单独使用,只是作为一个模板的功能。
  • 缺点:模板的通病,即影响代码的可读性。
  • 静态多态:CRTP 可以实现静态多态,但本质上 CRTP中的多个派生类并不是同一个基类,因此严格意义上不能叫多态。

3. CRTP示例

动态绑定

#include <iostream>
#include <typeinfo>
#include <sys/time.h>

class Base 
{
public:
	virtual void PrintType () const
	{
		std::cout << typeid(*this).name() << std::endl;
	}
};

class Derivedl : public Base {};
class Derived2: public Base {};

void PrintType(const Base& base)
{
	base.PrintType();
}

int main()
{
	Derivedl d1;
	Derived2 d2
	PrintType(d1);
	PrintType(d2);
}

“静态多态”

#include <iostream>
#include <typeinfo>
#include <sys/time.h>

template<typename T>
class Base
{
public:
	void PrintType ()
	{
		T& t = static cast<T&>(*this);
		std::cout << typeid(t).name() << std::endl;
	}
};

class Derivedl : public Base <Derivedl> {};
class Derived2 : public Base <Derived2> {};

template<typename T>
void PrintType(T base)
{
	base.PrintType();
}
int main()
{
	Derivedl d1;
	Derived2 d2
	PrintType(d1);
	PrintType(d2);
}

4. CRTP应用

enable_shared_from_this
某个类想返回智能指针版的this时,需要该类继承enable_shared_from_this,通过shared_from_this()返回对应智能指针。

	// CLASS TEMPLATE enable_shared_from_this
template<class _Ty>
	class enable_shared_from_this
	{	// provide member functions that create shared_ptr to this
public:
	using _Esft_type = enable_shared_from_this;
 
	_NODISCARD shared_ptr<_Ty> shared_from_this()
		{	// return shared_ptr
		return (shared_ptr<_Ty>(_Wptr));
		}
 
	_NODISCARD shared_ptr<const _Ty> shared_from_this() const
		{	// return shared_ptr
		return (shared_ptr<const _Ty>(_Wptr));
		}
 
	_NODISCARD weak_ptr<_Ty> weak_from_this() noexcept
		{	// return weak_ptr
		return (_Wptr);
		}
 
	_NODISCARD weak_ptr<const _Ty> weak_from_this() const noexcept
		{	// return weak_ptr
		return (_Wptr);
		}

参考链接:
C++:智能指针(5)——enable_shared_from_this工作原理、源码分析

低功耗蓝牙项目,需要一块懂省电的板

思澈 SF32LB52 芯片,BLE 协议栈深度优化,上手即开发

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值