functor

Function passed as template argument

瘦欲@ 提交于 2019-11-26 01:36:50
问题 I\'m looking for the rules involving passing C++ templates functions as arguments. This is supported by C++ as shown by an example here: #include <iostream> void add1(int &v) { v+=1; } void add2(int &v) { v+=2; } template <void (*T)(int &)> void doOperation() { int temp=0; T(temp); std::cout << \"Result is \" << temp << std::endl; } int main() { doOperation<add1>(); doOperation<add2>(); } Learning about this technique is difficult, however. Googling for \"function as a template argument\"

Good examples of Not a Functor/Functor/Applicative/Monad?

筅森魡賤 提交于 2019-11-26 00:31:18
问题 While explaining to someone what a type class X is I struggle to find good examples of data structures which are exactly X. So, I request examples for: A type constructor which is not a Functor. A type constructor which is a Functor, but not Applicative. A type constructor which is an Applicative, but is not a Monad. A type constructor which is a Monad. I think there are plenty examples of Monad everywhere, but a good example of Monad with some relation to previous examples could complete the

What are C++ functors and their uses?

眉间皱痕 提交于 2019-11-25 21:38:33
问题 I keep hearing a lot about functors in C++. Can someone give me an overview as to what they are and in what cases they would be useful? 回答1: A functor is pretty much just a class which defines the operator(). That lets you create objects which "look like" a function: // this is a functor struct add_x { add_x(int x) : x(x) {} int operator()(int y) const { return x + y; } private: int x; }; // Now you can use it like this: add_x add42(42); // create an instance of the functor class int i =