#include<iostream>#include<memory>usingnamespacestd;usingstd::unique_ptr;template<classType>autoup(Type*p){returnunique_ptr<Type>(p);}classTop{private:virtualTop*virtual_clone()const=0;public:unique_ptr<Top>clone()const{returnup(virtual_clone());}virtual~Top()=default;// Necessary for `delete` via Top*.};classD:publicTop{private:/* ← Covariant return */D*virtual_clone()constoverride{returnnewD(*this);}public:/* ← Apparent covariant return */unique_ptr<D>clone()const{returnup(virtual_clone());}};classDD:publicD{private:intanswer_=42;/* ← Covariant return */DD*virtual_clone()constoverride{returnnewDD(*this);}public:intanswer()const{returnanswer_;}/* ← Apparent covariant return */unique_ptr<DD>clone()const{returnup(virtual_clone());}};intmain(){autop1=unique_ptr<DD>(newDD());autop2=p1->clone();// Correct dynamic type DD for *p2 is guaranteed by the static type checking.cout<<p2->answer()<<endl;// "42"// Cleanup is automated via unique_ptr.}