6

Is there any way in C++ to get a pointer to a built-in arithmetic operator? Something like this doesn't compile:

const auto addition = static_cast<int(*)(int, int)>(operator+);

A simple workaround of this problem is to make an auxiliary lambda:

const auto addition = +[](const int a, const int b){ return a + b; };

But is there any other solution?

user0042
  • 7,917
  • 3
  • 24
  • 39
Constructor
  • 7,273
  • 2
  • 24
  • 66
  • 3
    Why was this question downvoted? – SergeyA Dec 14 '17 at 18:30
  • Interesting question in its own right but just in case it would help if you described why you need this. – Konrad Rudolph Dec 14 '17 at 18:30
  • @SergeyA I don't know. :-( – Constructor Dec 14 '17 at 18:31
  • @KonradRudolph I simply don't want to make lambdas for all built-in operators I want to have function pointers to. I'm too lazy... – Constructor Dec 14 '17 at 18:32
  • 4
    *Why* do you want to do this? What is the *actual* problem you want to solve? This question is a typical [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), where you ask for help with a solution to a for us unknown problem. What if there are *other* solutions? – Some programmer dude Dec 14 '17 at 18:34
  • @SergeyA Low research efforts obviously. – user0042 Dec 14 '17 at 18:34
  • @user0042 [This question](https://stackoverflow.com/questions/17644816/is-it-possible-to-get-the-function-pointer-of-a-built-in-standard-operator) is the first link in the Google search output for the query "c++ pointer to built-in operator". I should check it before asking my question, of course. – Constructor Dec 14 '17 at 18:42
  • @Constructor TBF: You couldn't know this particular question exists (it's not very popular), though I do. – user0042 Dec 14 '17 at 18:45

0 Answers0