function-object

How does the template parameter of std::function work? (implementation)

人盡茶涼 提交于 2019-11-27 04:59:57
问题 In Bjarne Stroustrup 's home page (C++11 FAQ): struct X { int foo(int); }; std::function<int(X*, int)> f; f = &X::foo; //pointer to member X x; int v = f(&x, 5); //call X::foo() for x with 5 How does it work? How does std::function call a foo member function ? The template parameter is int(X*, int) , is &X::foo converted from the member function pointer to a non-member function pointer ?! (int(*)(X*, int))&X::foo //casting (int(X::*)(int) to (int(*)(X*, int)) To clarify: I know that we don't

why function objects should be pass-by-value

不问归期 提交于 2019-11-27 01:26:57
问题 I have just read the classic book "Effective C++, 3rd Edition", and in item 20 the author concludes that built-in types, STL iterators and function object types are more appropriate for pass-by-value . I could well understand the reason for built-in and iterators types, but why should the function object be pass-by-value , as we know it is class-type anyway? 回答1: In a typical case, a function object will have little or (more often) no persistent state. In such a case, passing by value may no

creating a function object from a string

帅比萌擦擦* 提交于 2019-11-26 23:15:55
问题 Question: is there a way to make a function object in python using strings? Info: I'm working on a project which i store data in a sqlite3 server backend. nothing to crazy about that. a DAL class is very commonly done through code generation because the code is so incredibly mundane. But that gave me an idea. In python when a attribute is not found, if you define the function " getattr " it will call that before it errors. so the way I figure it, through a parser and a logic tree I could

When do you use function objects in C++?

戏子无情 提交于 2019-11-26 21:20:19
问题 I see function objects used often together with STL algorithms. Did function objects came about because of these algorithms? When do you use a function object in C++? What is its benefits? 回答1: As said jdv, functors are used instead of function pointers, that are harder to optimize and inline for the compiler; moreover, a fundamental advantage of functors is that they can easily preserve a state between calls to them 1 , so they can work differently depending on the other times they have been

How can it be useful to overload the “function call” operator?

百般思念 提交于 2019-11-26 16:06:43
问题 I recently discovered that in C++ you can overload the "function call" operator, in a strange way in which you have to write two pair of parenthesis to do so: class A { int n; public: void operator ()() const; }; And then use it this way: A a; a(); When is this useful? 回答1: This can be used to create "functors", objects that act like functions: class Multiplier { public: Multiplier(int m): multiplier(m) {} int operator()(int x) { return multiplier * x; } private: int multiplier; }; Multiplier

Build a function object with properties in TypeScript

和自甴很熟 提交于 2019-11-26 12:59:50
问题 I want to create a function object, which also has some properties held on it. For example in JavaScript I would do: var f = function() { } f.someValue = 3; Now in TypeScript I can describe the type of this as: var f: { (): any; someValue: number; }; However I can\'t actually build it, without requiring a cast. Such as: var f: { (): any; someValue: number; } = <{ (): any; someValue: number; }>( function() { } ); f.someValue = 3; How would you build this without a cast? 回答1: So if 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 =