1
假設我有使用傳統的繼承下面簡單的類層次結構:CRTP基地推導換算
struct A_classic {};
struct B_classic : A_classic {};
我想實現一個轉換操作符從A_classic
到B_classic
。要重用的代碼量可能,我做
A_classic a; // Given as input argument
B_classic b;
static_cast<A_classic&>(b) = a; // Copy A_classic's members
// Now set up B_classic's members
的問題是,實際上我使用CRTP繼承:
template<class Derived> struct A_crtp_base {};
struct A_crtp : A_crtp_base<A_crtp> {};
template<class Derived> struct B_crtp_base : A_crtp_base<B_crtp_base<Derived>> {};
struct B_crtp : B_crtp_base<B_crtp> {};
上述伎倆不再起作用,因爲「普通」基分別爲A_crtp
,B_crtp
分別爲A_crtp_base<A_crtp>
和A_crtp_base<B_crtp>
。
A_crtp a;
B_crtp b;
static_cast<A_crtp_base<???>&>(b) = a;
// No matter what I put here, either the cast or the assignment will fail
一個顯而易見的解決方案是模板的A_crtp_base
拷貝構造函數:
template<class Derived>
struct A_crt_base {
template<class OtherDerived>
A_crtp_base(const A_crtp_base<OtherDerived>& other);
}
但後來我不得不寫我自己的拷貝構造函數,這是我想避免的。
任何建議如何最大限度地減少編碼在這裏?