On Wednesday, 27 April 2016 14:50:10 UTC+3, Adi Shavit wrote:
> Is it possible to create a lambda that behaves like an operator
> overload?
No, not even as 'operator()' overload for something else since those
are totally different things. Lambda is an object of anonymous class
that has its own 'operator()' overload. That is it.
> That is, instead of calling it with the operator() syntax, it will be
> called by some other, e.g. binary, infix 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.
>
> 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 am sort of unsure what you want to accomplish. Perhaps you need
to start with motivating example problem. Do you want generic
lambdas of C++14? Those are simple. It is like that:
auto glambda = [] (auto a) { return a; };
We made a 'glambda' variable that contains an object of this type:
class /* unnamed */
{
public:
template<typename T>
T operator () (T a) const { return a; }
};
Now if you want you can call that 'glambda' from some template operator.