问题描述
#include <iostream>
#include <unordered_map>
#include <utility>
#include <typeinfo>
using namespace std;
class Handle{
public:
int val;
bool getAskPrice(int& tmp) const
{
tmp = val;
return true;
}
bool setAskPrice(int& tmp)
{
val = tmp;
return true;
}
};
template<class RT,class ARG>
struct convertToAFL{
static RT to_afl(ARG);
};
template<class RT,class ARG>
struct convertFromAFL{
static RT from_afl(ARG);
};
template<>
struct convertToAFL<float,int>
{
static float to_afl(int& value)
{
return static_cast<float>(value);
}
};
template<>
struct convertFromAFL<int,float>
{
static int from_afl(float& val)
{
return static_cast<int>(val);
}
};
struct Getter{
template<typename TICK_D,bool (Handle::*Getter)(TICK_D&) const,typename AFL_D>
static AFL_D getter(const Handle& handle)
{
TICK_D temp;
bool exists;
exists = (handle.*Getter)(temp);
AFL_D x = convertToAFL<AFL_D,TICK_D>::to_afl(temp);
return exists ? x : -1;
}
};
struct Setter{
template<typename TICK_D,bool (Handle::*Setter)(TICK_D&),typename AFL_D>
static void setter(Handle& handle,AFL_D& val)
{
TICK_D x;
x = convertFromAFL<TICK_D,AFL_D>::from_afl(val);
(handle.*Setter)(x);
}
};
int main()
{
Handle h;
float val = 20.0;
Setter::setter<int,&Handle::setAskPrice,float>(h,val);
std::cout<<Getter::getter<int,&Handle::getAskPrice,float>(h);
//std::pair<,&Setter::setter<int,float>> x;
return 0;
}
上面的代码按预期工作,但是在main()
中而不是在调用函数时,如何存储指向模板化的Setter:setter()
和Getter::getter()
的指针?
我正在尝试类似
std::pair<&Setter::setter<int,float>,&Getter::getter<int,float>(h)> func_pair;
并且以后可以调用这些函数。
但是我说错了
main.cpp: In function ‘int main()’:
main.cpp:85:118: error: type/value mismatch at argument 1 in template parameter list for ‘template struct std::pair’
std::pair<&Setter::setter<int,float>(h)> func_pair;
^
main.cpp:85:118: note: expected a type,got ‘& setter’
main.cpp:85:118: error: template argument 2 is invalid
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)