多态实现之CRTP

多态实现之CRTP
文章目录

你好,我是雨乐!

在之前的文章多态实现-虚函数、函数指针以及变体一文中,提到了多态的几种实现方式,今天,借助这篇文章,聊聊多态的另外一种实现方式CRTP

概念

CRTPCuriously Recurring Template Pattern的缩写,中文译为奇异的递归模板模式,是由James O. Coplien在其1995年的论文中首次提出。

wikipedia给出了CRTP的一般形式,如下:

 // The Curiously Recurring Template Pattern (CRTP)
template <class T>
class Base
{
    // methods within Base can use template to access members of Derived
};
class Derived : public Base<Derived>
{
    // ...
};

看到这种定义方式,你一定感到很奇怪,其实,这是种方式称为继承自模板技术。有一定编程经验的人,如果对于智能指针比较熟悉,那么已经无意中接触过这种技术,如下例子:

class Test: public std::enable_shared_from_this<Test> {
  // ...
};

如果与wikipedia给出的CRTP做对比,在这个示例中,Test对应于CRTP形式中的Derive的,而std::enable_shared_from_this()则对应于Base类。

那么,这样做的好处或者目的是什么呢?

其实,这样做的目的其实很明确,从基类对象的角度来看,派生类对象其实就是本身,这样的话只需要使用类型转换就可以把基类转化成派生类,从而实现基类对象对派生对象的访问。

为了便于更加清晰的理解,完整举例如下:

template <typename T>
class Base {
public:
    void interface() {
        static_cast<T*>(this)->imp();
    };
};

class Derived : public Base<Derived> {
public:
    void imp() {
        std::cout<< "in Derived::imp" << std::endl;  
    }
};

int main() {
  Derived b;
  d.interface();
  
  return 0;
}

在上述例子中,我们发现在Base类的interface接口中,使用static_cast进行类型转换,从而调用派生类的成员函数。可能会有人感到好奇,为什么不用dynamic_cast进行类型转换呢?主要是因为dynamic_cast应用于运行时,而模板是在编译器就进行了实例化

编译运行,输出结果如下:

in Derived::imp

从上面的输出结果可以看出,即使我们没有声明virtual函数,也实现了多态的功能。

截止到此,我们对CRTP有了一个初步的认识,总结起来,其有以下两个特点:

  • • 继承自模板类
  • • 派生类将自身作为参数传给模板类

颠倒继承

仍然使用上一节中的例子,如下:

template <typename T>
class Base {
public:
    void interface() {
        static_cast<T*>(this)->impl();
    };
};

class Derived : public Base<Derived> {
public:
    void imp() {
        std::cout << "in Derived::imp" << std::endl;  
    }
};

int main() {
  Base<Derived> b;
  d.interface();
  
  return 0;
}

在这个例子中,派生类Derived中定义了一个成员函数imp(),而该函数在基类Base中是没有声明的,所以,我们可以理解为对于CRTP,在基类中调用派生类的成员函数。而对于virtual,则是派生类中调用基类的成员函数,这就是我们所说的颠倒继承

使用场景

俗话说,存在即合理。既然有CRTP,那么其必然有自己存在的道理。那么CRTP都用在什么场景呢?

静态多态

其实,在前面的例子中,已经大致了解了使用crtp技术来实现多态功能,该种实现方式为静态多态,是一种编译期多态,下面通过一个具体的例子来理解静态多态。

#include <iostream>

template <typename T>
class Base{
 public:
  void interface(){
    static_cast<T*>(this)->imp();
  }
  void imp(){
    std::cout << "in Base::imp" << std::endl;
  }
};

class Derived1 : public Base<Derived1> {
 public:
  void imp(){
    std::cout << "in Derived1::imp" << std::endl;
  }
};

class Derived2 : public Base<Derived2> {
 public:
  void imp(){
    std::cout << "in Derived2::imp" << std::endl;
  }
};

struct Derived3 : public Base<Derived3>{};

template <typename T>
void fun(T& base){
    base.interface();
}


int main(){
  Derived1 d1;
  Derived2 d2;
  Derived3 d3;

  fun(d1);
  fun(d2);
  fun(d3);

  return 0;
}

在上述代码中,定义了一个函数fun(),在其函数体内调用interface()函数。如果类型为Derived1和Derived2,则会调用这俩类型对应的imp()函数。而对于Derived3,因为其类内没有实现imp()函数,所以调用的是base类即基类的imp函数。

编译运行之后,输出如下:

in Derived1::imp
in Derived2::imp
in Base::imp

从上述输出可以看出,即使不使用virtual,也实现了多态功能,其二者的区别是:virtual是运行时多态,而CRTP则是在编译期就对模板进行了实例化,所以属于动态多态。

代码复用

假如,现在需要实现一个功能,根据对象的具体类型,输出其类型名称。

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

class Derived : public Base {};
class Derived1 : public Base {};

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

对于此种需求,第一印象是使用virtual的多态功能实现,代码也很好写,如下:

#include <iostream>
#include <typeinfo>

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

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

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

int main() {
  Derived d;
  Derived1 d1;

  PrintType(d);
  PrintType(d1);

  return 0;
}

输出如下:

7Derived
8Derived1

ps: 需要注意的是,在上面的输出类型中,前面都有一个数字,这个数字是类名的长度

而如果我们使用CRTP呢,是不是可以避免virtual所带来的两个问题呢?实现如下:

#include <iostream>
#include <typeinfo>

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

class Derived : public Base<Derived> {};
class Derived1 : public Base<Derived1> {};

template<typename T>
void PrintType(base) {
  base.PrintType();
}

int main() {
  Derived d;
  Derived1 d1;

  PrintType(d);
  PrintType(d1);

  return 0;
}

函数输出如下:

7Derived
8Derived1

通过上述输出可以看出,即使在Derived和Derived1类中没有定义PrintType()函数,也实现了与virtual函数一样的效果,所以使用CRTP的另外一个好处是避免冗余代码

性能对比

既然crtp也能实现多态,那么就有必要跟传统的运行时多态实现方式virtual来做下对比。

virtual.cc:

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

class Base {
 public:
  virtual void PrintType() const {
  }
};

class Derived : public Base {
 public:
  virtual void PrintType() const {
  }
};
class Derived1 : public Base {
 public:
  virtual void PrintType() const {
  }
};

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

int main() {
  Derived d;
  Derived1 d1;

  struct timeval start;
  struct timeval end;
  gettimeofday(&start_, NULL);
  for (int i = 0; i < 1000000; ++i) {
    PrintType(d);
    PrintType(d1);
  }
  gettimeofday(&end, nullptr);
  double cost = 1000000 * (end.tv_sec - start.tv_sec) +
                  end.tv_usec - start.tv_usec;

  std::cout << "virtual time cost " << cost << std::endl;
  return 0;
}

crtp.cc:

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

template<typename T>
class Base {
 public:
  void PrintType() const {
    T &= static_cast<T&>(*this);
    t.PrintType();
  }
};

class Derived : public Base<Derived> {
 public:
  void PrintType() const {}
};
class Derived1 : public Base<Derived1> {
 public:
  void PrintType() const {}
};

template<typename T>
void PrintType(base) {
  base.PrintType();
}

int main() {
  Derived d;
  Derived1 d1;

  struct timeval start;
  struct timeval end;
  gettimeofday(&start, nullptr);
  for (int i = 0; i < 1000000; ++i) {
    PrintType(d);
    PrintType(d1);
  }
  gettimeofday(&end, NULL);
  double cost = 1000000 * (end.tv_sec - start.tv_sec) +
                  end.tv_usec - start.tv_usec;

  std::cout << "crtp time cost " << cost << std::endl;
  return 0;
}

对上述两段代码分别编译运行之后,输出如下:

virtual time cost 22757
crtp time cost 7871

从上述输出可以看出,crtp的耗时是virtual实现方式的1/3。

局限性

既然CRTP能实现多态性,且其性能优于virtual,那么virtual还有没有存在的必要么?

虽然CRTP最终还是调用派生类中的成员函数。但是,问题在于Base类实际上是一个模板类,而不是一个实际的类。因此,如果存在名为Derived和Derived1的派生类,则基类模板初始化将具有不同的类型。这是因为,Base类将派生自不同的特化,即 Base,代码如下:

class Derived : public Base<Derived> {
 void imp(){
    std::cout << "in Derived::imp" << std::endl;
  }
};

class Derived1 : public Base<Derived1> {
 void imp(){
    std::cout << "in Derived1::imp" << std::endl;
  }
};

如果创建Base类模板的指针,则意味着存在两种类型的Base指针,即:

// CRTP
Base<Derived> *= new Derived;
Base<Derived> *b1 = new Derived1;

显然,这与我们虚函数的方式不同。因为,动态多态性只给出了一种Base指针。但是现在,每个派生类都可以使用不同的指针类型。

// virtual
Base *v1 = new Derived;
Base *v2 = new Derived1;

正是因为基于CRTP方式的指针具有不同的类型,所以不能将CRTP基类指针存储在容器中,下面的代码将编译失败:

int main() {
  Base<Derived> *= new Derived;
  Base<Derived> *d1 = new Derived1;

  auto vec = {d, d1};

  return 0;
}

编译器输出如下:

test.cc: In function int main()’:
test.cc:33: error: cannot convert Derived1*’ to Base<Derived>*’ in initialization
test.cc:35: error: ISO C++ forbids declaration of vec with no type
test.cc:35: error: scalar object vec requires one element in initializer

正是因为其局限性,所以CRTP是一种特殊类型的多态性,在少数情况下可以替代动态多态性的需要。

输出结果完全符合预期,但是这样实现,可能存在以下两个问题:

• 性能损失:因为使用了virtual来实现此功能,而virtual函数会涉及到vtables等,所以如果频繁调用,性能会有影响

• 重复代码:为了实现这个功能,Derived和Derived1都需要在其函数体内实现PrintType()函数,如果派生类非常多的话,每个派生类都要实现该功能,冗余代码太多

结语

通过CRTP技术,在某种程度上也可以实现多态功能,但其也仅限定于使用场景,正如局限性一节中所提到的,CRTP是一种特殊类型的多态性,在少数情况下可以替代动态多态性的需要;另外,使用CRTP技术,代码可读性降低、模板实例化之后的代码膨胀以及无法动态绑定(在编译期决实例化),因此,我们可以根据使用场景,来灵活选择CRTP或者virtual来达到多态目的。

更多推荐

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/1118

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(1)
上一篇 2022年6月25日 下午11:46
下一篇 2022年7月8日 下午4:43

相关推荐