Is it possible to create a lambda that behaves like an operator
overload?
That is, instead of calling it with the operator() syntax, it will be
called by some other, e.g. binary, infix operator.
Is it possible to generically simulate this somehow just like lambdas
can as structs overloading the () operator. The problem is just how to
get the compiler/ADL to call the correct function based on the operator.
I don't see how. The point about a lambda is that it creates a thing
without a name (that you can then store in a named variable, but the thing itself is nameless), whereas operator overloading works precisely on the basis of the name of the (member)function.
It is indeed possible to make an operator overload that calls
whatever callable object (including lambda) stored in variable
somewhere for actually doing its work.
I don't know why one would want this kind of thing but
#include <iostream>
template<typename Callable>
struct bracketized: private Callable
{
bracketized(const Callable &c): Callable(c) {}
template<typename Param>
auto operator[](Param&& p)
-> decltype((*this)(std::forward<Param>(p)))
{
return (*this)(std::forward<Param>(p));
}
};
template<typename Callable>
bracketized<Callable> bracketize(const Callable &c)
{
return c;
}
int main()
{
int i = 1;
auto lambda = [&](int j) { i += j; };
auto b = bracketize(lambda);
b[42];
std::cout << i;
}
builds and runs OK with g++-4.7 -std=c++11.
With -O it even computes 43 at compile time.
Is it possible to create a lambda that behaves like an operator
overload?
That is, instead of calling it with the operator() syntax, it will be
called by some other, e.g. binary, infix operator.
Is it possible to generically simulate this somehow just like lambdas
can as structs overloading the () operator. The problem is just how to
get the compiler/ADL to call the correct function based on the operator.
It is indeed possible to make an operator overload that calls
whatever callable object (including lambda) stored in variable
somewhere for actually doing its work.
It is indeed possible to make an operator overload that calls
whatever callable object (including lambda) stored in variable
somewhere for actually doing its work.
I don't know why one would want this kind of thing but
#include <iostream>
template<typename Callable>
struct bracketized: private Callable
{
bracketized(const Callable &c): Callable(c) {}
template<typename Param>
auto operator[](Param&& p)
-> decltype((*this)(std::forward<Param>(p)))
{
return (*this)(std::forward<Param>(p));
}
};
template<typename Callable>
bracketized<Callable> bracketize(const Callable &c)
{
return c;
}
int main()
{
int i = 1;
auto lambda = [&](int j) { i += j; };
auto b = bracketize(lambda);
b[42];
std::cout << i;
}
builds and runs OK with g++-4.7 -std=c++11.
With -O it even computes 43 at compile time.
That's quite clever, though I am not sure this gives me
what I meant. In this case the bracket operator is essentially like the function
call operator.
I am more interested in overloading the arithmetic and logic
infix operators (+,+=, &, &=, etc) where one does not invoke the function explicitly.
Do you think this approach will work for that type of operator?
Could you "plusize" an add() lambda?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 72:51:03 |
Calls: | 6,657 |
Calls today: | 3 |
Files: | 12,203 |
Messages: | 5,332,378 |
Posted today: | 1 |