Functional Operator Pointer in C ++

So, I have a function that expects a function pointer as an input, and the prototype of which looks something like this:

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

I was wondering if I can pass an operator for this function pointer. In particular, this built-in statement:

 int operator|(int, int); 

I tried this:

 function(&(operator|)); 

and got this error:

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

I tried this:

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

and got this error:

 error: expected primary-expression before 'int' 

I looked for this situation in the documentation, but I get information about the "address" of the operator (operator &) instead of things about the address of the operator ....

Edit:

See the previous question Calling primitive operator functions explicitly in C ++

+8
c ++ operators function-pointers
source share
2 answers

Built-in operator | which takes two int and returns int , cannot be obtained via operator| . For example, the following does not compile

 int a = operator|(2,3); 
+3
source share

Since operators are not available as functions, the standard provides a number of functors corresponding to operators. The most famous of these are std::less , which is the default template option for sorting sorts, sets, and maps.

A complete list can be found in the functional header. std::bit_or to your requirements is std::bit_or .

Unfortunately, they are implemented as template classes with operator() and not functions, so they are not suitable for your specific use case. They are more suitable for templates.

+1
source share

All Articles