function-object

How to use a function of own class in for_each method?

无人久伴 提交于 2019-12-01 00:31:15
Assume I have this class (inherited from std::Vector, it's just an example) #include <vector> using namespace std; template <class T> class C : public vector<T> { // I don't want to use static keyword void transformation(T i) { i *= 100; } public: void method() { for_each(this->begin(), this->end(), transformation); } }; int main() { C<double> c; for (int i=-3; i<4; ++i) { c.push_back(i); } c.method(); } How do I call for_each using class method inside class itself? I know I can use static keyword, but what is there any other way how to use a function object without using static? I get this

How to use a function of own class in for_each method?

只谈情不闲聊 提交于 2019-11-30 19:44:34
问题 Assume I have this class (inherited from std::Vector, it's just an example) #include <vector> using namespace std; template <class T> class C : public vector<T> { // I don't want to use static keyword void transformation(T i) { i *= 100; } public: void method() { for_each(this->begin(), this->end(), transformation); } }; int main() { C<double> c; for (int i=-3; i<4; ++i) { c.push_back(i); } c.method(); } How do I call for_each using class method inside class itself? I know I can use static

Serializing function objects

时光毁灭记忆、已成空白 提交于 2019-11-30 07:59:35
问题 Is it possible to serialize and deserialize a std::function , a function object, or a closure in general in C++? How? Does C++11 facilitate this? Is there any library support available for such a task (e.g., in Boost)? For example, suppose a C++ program has a std::function which is needed to be communicated (say via a TCP/IP socket) to another C++ program residing on another machine. What do you suggest in such a scenario? Edit: To clarify, the functions which are to be moved are supposed to

Serializing function objects

纵然是瞬间 提交于 2019-11-29 05:50:03
Is it possible to serialize and deserialize a std::function , a function object, or a closure in general in C++? How? Does C++11 facilitate this? Is there any library support available for such a task (e.g., in Boost)? For example, suppose a C++ program has a std::function which is needed to be communicated (say via a TCP/IP socket) to another C++ program residing on another machine. What do you suggest in such a scenario? Edit: To clarify, the functions which are to be moved are supposed to be pure and side-effect-free. So I do not have security or state-mismatch problems. A solution to the

javascript class inherit from Function class

允我心安 提交于 2019-11-28 07:00:23
I like that in javascript, I can create a function, and then add further methods and attributes to that function myInstance = function() {return 5} myInstance.attr = 10 I would like to create a class to generate these objects. I assume I have to inherit from the Function base class. In other words, I would like to: var myInstance = new myFunctionClass() var x = myInstance() // x == 5 But I don't know how to create the myFunctionClass. I have tried the following, but it does not work: var myFunctionClass = function() {Function.call(this, "return 5")} myFunctionClass.prototype = new Function()

why function objects should be pass-by-value

北慕城南 提交于 2019-11-28 06:25:43
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? 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 require actually passing anything at all -- the "value" that's passed is basically little or nothing more

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

断了今生、忘了曾经 提交于 2019-11-28 02:50: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 need to cast any pointer to use std::function , but I don't know how the internals of std::function

creating a function object from a string

你。 提交于 2019-11-27 23:12:18
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 dynamically generate the code I need on its first call, then save the function object as a local attrib.

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

我只是一个虾纸丫 提交于 2019-11-27 12:42:59
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? 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 m(5); cout << m(4) << endl; The above prints 20 . The Wikipedia article linked above gives more

Build a function object with properties in TypeScript

白昼怎懂夜的黑 提交于 2019-11-27 07:10:14
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? So if the requirement is to simply build and assign that function to "f" without a cast, here is a possible solution: var f: {