#include <iostream>
template<class derived>
struct base
{
derived getDerivedType(){};
void interface(){ static_cast<derived*>(this)->interface();};
};
struct derived1:public base<derived1>
{
using base::getDerivedType;
void interface()
{
std::cout<< "derived1 interface \n";
}
};
struct derived2:public base<derived2>
{
void interface()
{
std::cout<< "derived2 interface \n";
}
};
template <class T>
struct interface_t {
base<T> operator()( base<T>& s) const
{
s.interface();
return s;
}
};
template<class T>
const interface_t<T> ext_interface ={};
template<class T>
base<T> operator|(base<T>& that,interface_t<T> ext)
{
return ext(that);
}
struct extension_interface
{
extension_interface& operator-(void)
{
return *this;
}
template <class T>
friend const base<T>& operator >(base<T>& that,extension_interface& ex)
{
that.interface();
return that;
}
};
int main()
{
derived1* d1 = new derived1();
derived2 d2;
//d2| ext_interface<decltype(d2.getDerivedType())> ; //boost这种扩展模板类不方便
d2>-extension_interface();
*d1>-extension_interface();
return 0;
}
标签:return,extension,递归,operator,base,奇异,interface,模板,struct
From: https://www.cnblogs.com/nocanstillbb/p/16897222.html