function-object

Most terse and reusable way of wrapping template or overloaded functions in function objects

≡放荡痞女 提交于 2021-01-28 21:31:47
问题 Scenario 1: a template function pred template<typename T> bool pred(T t) { /* return a bool based on t */ } Scenario 2: a set of functions overloaded on the same name pred bool pred(A t) { /* return a bool based on t */ } bool pred(B t) { /* return a bool based on t */ } bool pred(C t) { /* return a bool based on t */ } ... Whichever of the two scenarii we're in, the bottom line is that pred does not refer to a function, and so it cannot be passed around, e.g. as a unary predicate to std:

C++ Passing function objects as lvalues and/or rvalues

安稳与你 提交于 2021-01-27 07:22:41
问题 I have a class that should filter its contents according to a user-supplied predicate. The interface I am given prescribes taking a reference to the predicate: class Test { vector<int> data; public: template <class PREDTYPE> void filter(PREDTYPE& pred) { return; } }; I am also given a piece of test code, which looks roughly like this: class Filter { public: bool operator()(int) const { return false; } }; int main() { Test test; test.filter(Filter()); } This doesn’t compile, saying cannot bind

Recursively call a function object

孤人 提交于 2020-02-01 20:19:04
问题 How do I call a function object from within itself? Seems I cannot use this . Example: class factorial { public: int operator()(int n) { if (n == 0) return 1; return n * ??(n-1); } }; What do I place at ?? ? 回答1: #include <iostream> class factorial { public: int operator()(int n) { if (n == 0) return 1; return n * (*this)(n-1); } }; int main() { std::cout << factorial()(5) << std::endl; } Works fine for me. Live example. 回答2: You can either use the name of the overloaded operator: operator()

Why doesn't N3421 provide the noexcept qualifier?

て烟熏妆下的殇ゞ 提交于 2019-12-30 09:52:23
问题 In N3421 - Making Operator Functors greater<>, the new specialization for the std function objects is: template <> struct plus<void> { template <class T, class U> auto operator()(T&& t, U&& u) const -> decltype(std::forward<T>(t) + std::forward<U>(u)); }; instead of template <> struct plus<void> { template <class T, class U> auto operator()(T&& t, U&& u) const noexcept(noexcept(decltype(std::forward<T>(t) + std::forward<U>(u)) (std::move(std::forward<T>(t) + std::forward<U>(u))))) -> decltype

javascript class inherit from Function class

半腔热情 提交于 2019-12-28 12:05:12
问题 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

How is factorial computed?

亡梦爱人 提交于 2019-12-24 03:25:18
问题 say there is a function to calculate factorial(n) Does factorial(7) creates 7 function object for each of n from 1 to 7 and use those values when ever necessary (for factorial(8) as like factorial(7)*8) 回答1: It depends on the language and the language implementation. In many functional languages (e.g. Haskell), a function is guaranteed to change nothing; only to return a value. This lack of side effects allows the language to remember/cache, or "memoize", the results of function calls. In a

how to pass a class method as argument to another method of the class in perl 6

北城余情 提交于 2019-12-22 07:06:13
问题 I have a script like the below. Intent is to have different filter methods to filter a list. Here is the code. 2 3 class list_filter { 4 has @.my_list = (1..20); 5 6 method filter($l) { return True; } 7 8 # filter method 9 method filter_lt_10($l) { 10 if ($l > 10) { return False; } 11 return True; 12 } 13 14 # filter method 15 method filter_gt_10($l) { 16 if ($l < 10) { return False; } 17 return True; 18 } 19 20 # expecting a list of (1..10) to be the output here 21 method get_filtered_list

What is the correct argument type for a function-object?

荒凉一梦 提交于 2019-12-22 04:14:06
问题 I have a templated function that receives function-objects. Sometimes the function-objects are stateless structs, but sometimes they are large statefull objects. The state of the function-object is not changed in this function, only examined. I'm also very keen on writing code that the compiler can optimize as much as possible. What should I consider when choosing the argument type? The function is of this type: template<typename funcT> auto make_particle(funcT fun) { Particle<typename funcT:

Detailed difference between functor's call and function call?

本秂侑毒 提交于 2019-12-22 02:30:08
问题 The key reason this works is that for_each () doesn’t actually assume its third argument to be a function. It simply assumes that its third argument is something that can be called with an appropriate argument. A suitably defined object serves as well as – and often better than – a function. For example, it is easier to inline the application operator of a class than to inline a function passed as a pointer to function. Consequently, function objects often execute faster than do ordinary

how to pass a class method as argument to another method of the class in perl 6

我们两清 提交于 2019-12-13 00:58:55
问题 I have a script like the below. Intent is to have different filter methods to filter a list. Here is the code. 2 3 class list_filter { 4 has @.my_list = (1..20); 5 6 method filter($l) { return True; } 7 8 # filter method 9 method filter_lt_10($l) { 10 if ($l > 10) { return False; } 11 return True; 12 } 13 14 # filter method 15 method filter_gt_10($l) { 16 if ($l < 10) { return False; } 17 return True; 18 } 19 20 # expecting a list of (1..10) to be the output here 21 method get_filtered_list