目前在我的代碼中,我有一個函式,它需要各種引數和一個回呼物件(它是一個具有 Callback(void) 函式的簡單類的實體)。這一點現在運行得非常好,但是它使代碼變得混亂,因為我有很多這樣的類,而在大多數類中,它們的變數只被 Callback() 函式使用。
我想用簡單的函式來代替這些類,就像這樣:
我想用簡單的函式來代替這些類。
#include <iostream>
#include <string>
template<class T>
class TestClass {
public:
template<typename ...TArgs>
using CallbackFuncType = bool(T &Data, TArgs & & ...Args)。
TestClass(const T &Data) : data(Data) {} 。
template<typename ...TCrgs>
bool Function(
CallbackFuncType<TCrgs...> Callback,
TArgs && ...Args
)
{
return Callback(資料。
std::forward<TArgs>(Args) ...)。
}
private:
const T data。
};
struct DataType {
int valInt;
double valFloat;
std::string ToString()
{
return std::string(
"valInt: " std::to_string(valInt)
"valFloat: "/span> std::to_string(valFloat)
);
}
};
bool CBFunction1(DataType &Data, int Arg0, const std: :string &Arg1)
{
std::cout << "Data :" << Data.ToString() << std::endl。
std::cout << "CBFunction1: " <<
"Arg0 : " << Arg0 <<
"Arg1: " << Arg1 <<
std::endl;
return true。
}
bool CBFunction2(DataType & Data, int Arg0, double Arg1, const std: :string &Arg2)
{
std::cout << "Data :" << Data.ToString() << std::endl。
std::cout << "CBFunction2: " <<
"Arg0 : " << Arg0 <<
"Arg1: " << Arg0 <<
"Arg2: " << Arg2 <<
std::endl;
return true。
}
int main()
{
資料型別data。
data.valInt = 1;
data.valFloat = 1.2;
TestClass<DataType> testObj(data)。
std::cout << "test" << std::endl。
std::cout << "呼叫CBFunction1" << std::endl。
bool val1 = testObj.Function(CBFunction1, 11, "test_string") 。
bool val2 = testObj.Function(CBFunction2, 11, 111.111, "test_string") 。
return 0;
在編譯時,我得到了以下錯誤:
在編譯時,我得到了以下錯誤。
1> main.cpp
1>main.cpp(75,24): 錯誤C2672。'TestClass<DataType>:Function': 沒有找到匹配的多載函式
1>main.cpp(75,64): 錯誤C2784。'bool TestClass<DataType>::Function(bool (__cdecl *)(T &,TArgs &...),TArgs & & .... )': 無法推匯出模板引數for 'bool (__cdecl *)(T & ,TArgs &&. ...)'來自'bool (DataType &,int,const std::string &)'
1> with
1> [
1> T=DataType
1> ]。
1>main.cpp(13): message : see declaration of 'TestClass< DataType> :Function'
1>main.cpp(76,24) : 錯誤C2672。'TestClass<DataType>:Function': 沒有找到匹配的多載函式
1>main.cpp(76,73): 錯誤C2784。'bool TestClass<DataType>::Function(bool (__cdecl *)(T &,TArgs &...),TArgs & & .... )': 無法推匯出模板引數for 'bool (__cdecl *)(T & ,TArgs &&. ...)'來自'bool (DataType &,int,double,const std::string &)'
1> with
1> [
1> T=DataType
1> ]。
1>main.cpp(13): message : see declaration of 'TestClass< DataType> :Function'
我試圖在Args中添加type_identity_t,但沒有成功。
uj5u.com熱心網友回復:
template <typename... TArgs>
bool Function(CallbackFuncType<TArgs...> Callback, TArgs&& ... Args)
有幾個問題,主要的問題是TArgs在兩個地方的推導應該是完全一樣的。
問題在于
template <typename...。TArgs>
using CallbackFuncType = bool(T& Data, TArgs& & ... Args)。
是它只能匹配引數為(l或r值)參考的函式。
所以類似于
的東西template <typename... TArgs>
using CallbackFuncType = bool(T& , TArgs...)。
template <typename...。TArgs, typename...。Ts>。
bool Function(CallbackFuncType<TArgs...>* Callback, Ts& & ...Args)
{
return Callback(data, std::forward<Ts>(Args) ...)。
}
可以解決這個問題,但更簡單(而且更通用)
template <typename Func, typename... 解釋。
bool Function(Func Callback, Ts&& ...Args)
{
return Callback(data, std::forward<Ts>(Args) ...)。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/314105.html
標籤:
