私の質問に関連する例を探していましたが、まだ解決策が見つかりません。私が見つけた最も近いものは
必要に応じて実際の例を投稿しようとしますが、これまでのコードの一部には次のものが含まれています。
template<class InterfaceType, class T>
inline void write_info(InterfaceType& interface, T& t) {
InterfaceType::write_info(interface, t);
}
template<class InterfaceType, class T>
inline void write_data(InterfaceType& interface, T& t) {
InterfaceType::write_data(interface, t);
}
template<class InterfaceType, class T>
inline void write_definition(InterfaceType& interface, T& t) {
InterfaceType::write_definition(interface, t);
}
テンプレートは、 (静的メソッド)write_info
と呼ばれるメソッドを持つインターフェイス タイプに依存することに注意してください。write_info
これが行われる理由は、write_info
後で何も再定義しなくても、特定のデータ型に関数を特化できるためInterfaceType
です。
簡単な質問は次のとおりです。関数を関数パラメーターとして指定するテンプレートを使用して、上記のコードを削減できますか? 特殊なデータ型に対してこれら 3 つの関数をすべて定義することを避けることができるように、これを可能にしたいということを心に留めておいてください。
がとの 2 つの属性をfoo
持つ構造体であるとします。次に、上記の関数を次のように特殊化できます。int a
double b
template<class InterfaceType>
inline void write_info(InterfaceType& interface, foo& t) {
InterfaceType::write_info(interface, t.a);
InterfaceType::write_info(interface, t.b);
}
template<class InterfaceType>
inline void write_data(InterfaceType& interface, foo& t) {
InterfaceType::write_data(interface, t.a);
InterfaceType::write_data(interface, t.b);
}
template<class InterfaceType>
inline void write_definition(InterfaceType& interface, foo& t) {
InterfaceType::write_definition(interface, t.a);
InterfaceType::write_definition(interface, t.b);
}
ご覧のとおり、同じコードを何度も書いています。write_info
ここでは、InterfaceType にすでに define 、for 、write_data
およびがあると想定しています。何か案は?write_definition
int
double