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
?
Asked
Active
Viewed 87 times
0

Levon Muradyan
- 47
- 6
-
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 -
3Perhaps the functional operators, e.g. plus
, minus – EvertW Oct 18 '18 at 13:46etc. would be more useful for your purpose? -
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 Answers
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
-
2Please do not use backticks `\`` for codeblocks, but instead use four spaces in front of each code line. – hellow Oct 18 '18 at 13:53
-
1what would be the type of `vector<>` ? I am not familiar at all with that new fancy deduction stuff, but you need to declare the type somewhere, no? – 463035818_is_not_an_ai Oct 18 '18 at 15:00
-