0

Do overloading operators have address,that i can put them in vector, for example like this vector<int> v = { &operator(+),&operator(-),&operator(*),&operator(/) };,and then if i write this int a= 1(*v[0])2 it equals to a=1+2=3?

  • Actually, yeah, just not for builtins. The overloaded operators are basically just functions. Can you clarify if you meant the builtin operators, or the overloaded ones? – Bartek Banachewicz Oct 18 '18 at 13:42
  • overloaded ones – Levon Muradyan Oct 18 '18 at 13:45
  • 1) "_for example like this `vector v = { &operator(+),&operator(-),&operator(*),&operator(/) };`_" This wouldn't compile, since the actual type of `operator+`, depends on its definition, but it would never be equal to `int`. In addition to the fact, that there's no such thing as `operator(+)` (it's `operator+`). 2) "_if i write this `int a= 1(*v[0])2` it equals to `a=1+2=3`?_" This wouldn't compile either. Usage would need to be as a regular function pointer. – Algirdas Preidžius Oct 18 '18 at 13:46
  • 3
    Perhaps the functional operators, e.g. plus, minus etc. would be more useful for your purpose? – EvertW Oct 18 '18 at 13:46
  • You can wrap overloaded (or non-overloaded) operators in a lambda, `auto v = vector>{ [](Foo const& rhs, Foo const& lhs) { return rhs + lhs; } };` – Eljay Oct 18 '18 at 14:05
  • OP: "overloaded ones". vote to reopen, as the dupe is about builtins only – 463035818_is_not_an_ai Oct 18 '18 at 15:07

1 Answers1

3

You can use pointers to function and store lambdas there. Something in this way:

auto plus = [](auto a, auto b) { return a + b; }
auto minus = [](auto a, auto b) { return a - b; }
...
vector<decltype(plus)> v = {plus, minus, ...}
Vladimir Berezkin
  • 3,580
  • 4
  • 27
  • 33