将运算符作为参数传递 [英] Passing operator as a parameter

查看:131
本文介绍了将运算符作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个计算2个布尔变量的函数(如真值表)

I want to have a function that evaluates 2 bool vars (like a truth table)

例如:

T | F:T

然后

myfunc('t', 'f', ||);  /*defined as: bool myfunc(char lv, char rv, ????)*/

应返回true;

如何传递第三个参数? (我知道可以将其作为char *传递,但随后我将不得不有另一个表来比较运算符字符串,然后执行我要避免的操作)

how can I pass the third parameter? (I know is possible to pass it as a char* but then I will have to have another table to compare operator string and then do the operation which is something I would like to avoid)

是否可以在函数/方法中传递诸如^(XOR)或||(OR)或&&(AND)等运算符?

Is it possible to pass an operator like ^(XOR) or ||(OR) or &&(AND), etc in a function/method?

预先感谢

推荐答案

声明:

template<class Func> bool myfunc(char lv, char rv, Func func);

或者如果您需要单独链接:

Or if you need to link it separately:

bool myfunc(char lv, char rv, std::function<bool(bool,bool)> func);

然后您可以致电:

myfunc('t', 'f', std::logical_or<bool>());

这篇关于将运算符作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆