By Zhihao Yuan
Callable Objects in Java, C#, Rust, and C++
It is trivial to make a class callable in C++. Just define operator(). But what if you cannot?
Adding operator() requires three things to check simultaneously: you’re working on an object of a class type, you own the class type, and the action of "call" has an unambiguous meaning in the class. You may think a closure or a bind_front object will solve all problems. But, depending on which pieces are missing, such a solution can range from adequate to a code smell.
The talk will begin with an old tale: Java — a language with no operator() and was transitioning to adding lambda. Then, we will look into how Java, C♯, and Rust avoid all the restrictions of operator() by not adding operator() to the language. Finally, we’ll introduce a small library change in C++ that achieves all the benefits we’ve seen and even more.