正如Sam注意到的,MFP
是一個模板,而std::map
的第二個模板參數需要一個類型。因此,在使用函數指針填充地圖之前,您需要獲取實際的類型。讓我提出這個案例最直接的方法 - 模板類。有了它,你將需要在對象實例化中列出所有需要的返回類型,但是你將能夠使用任何類型的Call
。我將使用std::function
而不是指針,但可以輕鬆回滾到函數指針。
首先,我們不知道類用戶需要多少類型,所以讓我們將其作爲可變參數。由於map
需要一個完整的類型,我們需要一堆地圖 - 每種類型一個。獲得它的最常見方法是一個元組,在我們的例子中需要擴展包。通過元組,我們可以在編譯時搜索需要的映射,然後在運行時按名稱搜索函數。看看代碼與解釋:
template<typename ...Types>
class B {
private:
// Template alias for std::function.
template<typename T>
using MFP = std::function<T()>;
/* Tuple of maps from std::string to MFP for all types
in Types parameter pack. */
std::tuple<std::map<std::string, MFP<Types>>...> fmap;
template<typename T>
T f() { return 2.5; }
template<typename T>
T g() { return 1.0f; }
// Call implementation with compile-time pattern matching.
// T is return type, U is current matching type
template<typename T, size_t idx, typename U, typename ...Ts>
struct CallImpl {
static T callImpl(B* this_ptr, const std::string & s) {
/* If we exhausted Ts pack, we have no proper instance for
requested return type. Let's print a human-readable
compilation error message. */
static_assert((sizeof ... (Ts)) > 0,
"Requested return type not found.");
/* Otherwise discard U, increment tuple index
and try the next type. */
return CallImpl<T, idx + 1, Ts...>::callImpl(this_ptr, s);
}
};
/* This partial specialization is called when return
* type (T in above declaration) matches
* stored type (U in above declaration). */
template<typename T, size_t idx, typename ...Ts>
struct CallImpl<T, idx, T, Ts...> {
static T callImpl(B* this_ptr, const std::string & s) {
/* First, get the map from tuple by index.
This operation is either always valid in runtime or does not compile.
Next, get function object from map. It may fail in runtime
if user passed invalid string, so consider using map::at
or add any other sensible logic for this case. */
return std::get<idx>(this_ptr->fmap)[s]();
}
};
public:
B() {
/* Populate map with objects. Ellipsis in the last line
expands Types as needed. */
fmap = std::make_tuple(std::map<std::string, MFP<Types>>{
{"f", std::bind(std::mem_fn(&B::f<Types>), this)},
{"g", std::bind(std::mem_fn(&B::g<Types>), this)}
}...);
}
template<typename T>
T Call(const std::string & s) {
/* Start pattern matching with zero index. */
return CallImpl<T, 0, Types...>::callImpl(this, s);
}
};
用法:
int main() {
B<int, float, short> a; // Provides int, float and short return types.
std::cout << a.Call<int>("f") << std::endl; // Prints 2, which is 2.5 casted to int.
std::cout << a.Call<float>("f") << std::endl; // Prints 2.5
// Compilation error with "Requested type not found." message among others.
std::cout << a.Call<double>("f") << std::endl;
}
一些注意事項:
在
2.5
f
聲明是雙重的文字,但雙不列在B<int, float> a;
中,我們在上得到編譯錯誤。
- 代碼
Call
方法和callImpl
函數short
根本不會生成,因爲我們沒有實例化它。
您必須指定模板參數,如'std :: map> fmap;'。否則''A :: fmap'可能會在剛剛寫入'A a;'時被實例化。 –
songyuanyao