C++ C+中运算符的函数指针+;

C++ C+中运算符的函数指针+;,c++,operators,function-pointers,C++,Operators,Function Pointers,所以我有一个函数,它需要一个函数指针作为输入,它的原型是这样的: int function(int (*op)(int, int)); 我想知道是否可以为该函数指针传入一个运算符。具体而言,此内置运算符: int operator|(int, int); 我试过这个: function(&(operator|)); function(&(operator|(int, int))); 得到了这样的错误: error: no matching function for cal

所以我有一个函数,它需要一个函数指针作为输入,它的原型是这样的:

int function(int (*op)(int, int));
我想知道是否可以为该函数指针传入一个运算符。具体而言,此内置运算符:

int operator|(int, int);
我试过这个:

function(&(operator|));
function(&(operator|(int, int)));
得到了这样的错误:

error: no matching function for call to 'function(<unresolved overloaded function type>)'
error: expected primary-expression before 'int'
得到了这样的错误:

error: no matching function for call to 'function(<unresolved overloaded function type>)'
error: expected primary-expression before 'int'
我在文档中查找过这种情况,但我得到的是关于“运算符的地址”(operator&)的信息,而不是关于运算符的地址的信息

编辑:

见上一个问题

无法通过表示法
运算符访问接受两个
int
并返回一个
int
的内置运算符。例如,以下内容不编译

int a = operator|(2,3);

由于运算符不能作为函数使用,因此标准提供了一系列与运算符对应的函子。其中最著名的是
std::less
,它是排序、集合和映射的默认模板参数

完整列表可在
功能
标题中找到。与您的需求最接近的是


不幸的是,这些都是用
operator()
而不是函数实现的模板类,因此它们不适合您的特定用例。它们更适合模板。

在函数调用中消除
&
有效吗?@SuvP为什么要这样做?该函数不存在,因此您不能获取其地址。@JamesKanze我以为他重载了它。@SuvP他不能。除非至少有一个操作数是用户定义的类型,否则不能重载运算符。这是因为函数不存在。内置运算符不是函数,即使它们的行为就像用于重载解析一样。所以,基本上,我需要创建一个函数包装器。。。int或(inta,intb){返回a | b;}@Ned:好的,那就行了。使用C++11,您还可以将函数指针替换为
std::function
,并传递lambda表达式。@user763305一旦转换为
std::function
,也可以使用
std::bit\u或
@Ned不调用它
——所有大写表示宏。不要称之为映射到
|