C++ 通过方法参数传递数学运算符的最佳方式
如问题所示,我有如下方法:C++ 通过方法参数传递数学运算符的最佳方式,c++,templates,math,methods,parameters,C++,Templates,Math,Methods,Parameters,如问题所示,我有如下方法: template<typename LN, typename RN, typename LV, typename RV> VariableValue dataArithmetic(LN leftNode, RN rightNode, LV leftV, RV rightV) { if (leftNode.type() == typeid(ChartData*) && rightNode.type() == typeid(Chart
template<typename LN, typename RN, typename LV, typename RV>
VariableValue dataArithmetic(LN leftNode, RN rightNode, LV leftV, RV rightV) {
if (leftNode.type() == typeid(ChartData*) && rightNode.type() == typeid(ChartData*)) {
ChartData* temp1 = boost::get<ChartData*>(leftNode);
ChartData* temp2 = boost::get<ChartData*>(rightNode);
temp1->data *= temp2->data; // <----------------
return temp1;
}
else if (leftNode.type() == typeid(ChartData*) || rightNode.type() == typeid(ChartData*)) {
if (leftNode.type() == typeid(ChartData*)) {
ChartData* temp = boost::get<ChartData*>(leftNode);
float value = boost::lexical_cast<float>(rightV);
temp->data *= value; // <----------------
return (VariableValue)temp;
}
else if (rightNode.type() == typeid(ChartData*)) {
ChartData* temp = boost::get<ChartData*>(rightNode);
float value = boost::lexical_cast<float>(leftV);
temp->data *= value; // <----------------
return (VariableValue)temp;
}
}
return 0;
}
我希望该方法采用一个参数,指定应应用哪个数学运算符,例如:
VariableValue dataArith = dataArithmetic(leftNode, rightNode, leftV, rightV, **MULTIPLICATION**);
执行此操作的最佳方法是什么?传递
std::binary_function
或只是std::function
在操作上设置函数模板:
template<typename LN, typename RN, typename LV, typename RV, typename OP>
VariableValue dataArithmetic(LN leftNode, RN rightNode, LV leftV, RV rightV, OP op) {
dataArithmetic(..., std::multiplies<>{})
dataArithmetic(..., std::plus<>{})
dataArithmetic(..., std::minus<>{})
dataArithmetic(..., std::divides<>{})
模板
可变值数据算术(LN leftNode、RN rightNode、LV leftV、RV rightV、OP){
然后通过适当的操作:
template<typename LN, typename RN, typename LV, typename RV, typename OP>
VariableValue dataArithmetic(LN leftNode, RN rightNode, LV leftV, RV rightV, OP op) {
dataArithmetic(..., std::multiplies<>{})
dataArithmetic(..., std::plus<>{})
dataArithmetic(..., std::minus<>{})
dataArithmetic(..., std::divides<>{})
数据算术(…,std::乘{})
数据算术(…,std::plus{})
数据算术(…,std::减号{})
数据算术(…,std::除法{})
在C++14之前,标准库中的那些函数对象需要模板参数(
std::multiples{}
)。C++14或之后,它们不需要模板参数。感谢Jeff Garrett提供的指针
解决方案:
template<typename LN, typename RN, typename LV, typename RV, typename OP>
VariableValue dataArithmetic(LN leftNode, RN rightNode, LV leftV, RV rightV, OP op) {
if (leftNode.type() == typeid(ChartData*) && rightNode.type() == typeid(ChartData*)) {
ChartData* temp1 = boost::get<ChartData*>(leftNode);
ChartData* temp2 = boost::get<ChartData*>(rightNode);
//;
ChartData* temp = new ChartData(op(temp1->data, temp2->data));
return temp1;
}
else if (leftNode.type() == typeid(ChartData*) || rightNode.type() == typeid(ChartData*)) {
if (leftNode.type() == typeid(ChartData*)) {
ChartData* temp1 = boost::get<ChartData*>(leftNode);
float value = boost::lexical_cast<float>(rightV);
ChartData* temp = new ChartData(op(temp1->data, value));
return (VariableValue)temp;
}
else if (rightNode.type() == typeid(ChartData*)) {
ChartData* temp1 = boost::get<ChartData*>(rightNode);
float value = boost::lexical_cast<float>(leftV);
ChartData* temp = new ChartData(op(temp1->data, value));
return (VariableValue)temp;
}
}
return 0;
}
模板
可变值数据算术(LN leftNode、RN rightNode、LV leftV、RV rightV、OP){
if(leftNode.type()==typeid(ChartData*)和&righnode.type()==typeid(ChartData*)){
ChartData*temp1=boost::get(leftNode);
ChartData*temp2=boost::get(rightNode);
//;
ChartData*temp=新的ChartData(op(temp1->data,temp2->data));
返回temp1;
}
else if(leftNode.type()==typeid(ChartData*)| | rightNode.type()==typeid(ChartData*)){
if(leftNode.type()==typeid(ChartData*)){
ChartData*temp1=boost::get(leftNode);
浮点值=boost::词法转换(rightV);
ChartData*temp=新的ChartData(op(temp1->data,value));
返回(可变值)温度;
}
else if(rightNode.type()==typeid(ChartData*)){
ChartData*temp1=boost::get(rightNode);
浮点值=boost::词法转换(leftV);
ChartData*temp=新的ChartData(op(temp1->data,value));
返回(可变值)温度;
}
}
返回0;
}
致电:
VariableValue dataArith = dataArithmetic(leftNode, rightNode, leftV, rightV, std::multiplies<>{});
VariableValue dataArith=data算术(leftNode,righnode,leftV,rightV,std::乘{});
谢谢你的回答,但是我如何使用OP
来代替temp1->data*=temp2->data;
?