c++ - C++中运算符的函数指针

标签 c++ operators function-pointers

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

int function(int (*op)(int, int));

我想知道是否可以为该函数指针传递一个运算符。特别是这个内置运算符:

int operator|(int, int);

我已经试过了:

function(&(operator|));

得到了这样的错误:

error: no matching function for call to 'function(<unresolved overloaded function type>)'

我已经试过了:

function(&(operator|(int, int)));

得到了这样的错误:

error: expected primary-expression before 'int'

我在文档中查找过这种情况,但我得到的是关于“地址”运算符 (operator&) 的信息,而不是关于运算符地址的信息....

编辑:

见上一个问题 Calling primitive operator-functions explicitly in C++

最佳答案

内置运算符 |需要两个 int 并返回一个 int 不能通过符号 operator| 访问。例如,以下不编译

int a = operator|(2,3);

关于c++ - C++中运算符的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17299933/

相关文章:

C++ 显式声明在默认构造函数中触发警告

c++ - C++ 中同名的 C 结构体和 C 函数

math - 为什么 ( x & 3 ) 与 ( x mod 4 ) 相同?

c++ - 奇怪的逗号运算符行为

c - pthread_create 的正确参数是什么

c++ - 类型转换内存 union

c++ - 迭代器的大小与其容器的大小正相关吗?

c++ - 在代码片段 ":"中解释 C++ 中 "int i:2;"运算符的使用

c++ - 将函数指针转换为 noexcept 指定的函数指针

c++ - 从 JIT 代码调用方法