function-object

Are function pointers function objects in C++?

守給你的承諾、 提交于 2019-12-12 07:36:52
问题 The C++ standard defines function objects as: A function object type is an object type that can be the type of the postfix-expression in a function call. (link) First I was thinking that function objects were functors, but then I realized that for a function pointer ptr of type P (not a function, but a function pointer), std::is_object_v<P> is true and can be called with the ptr(Args...) syntax. I am right that function pointers are considered as function objects by the standard? And if they

Function wrapper via (function object) class (variadic) template

两盒软妹~` 提交于 2019-12-11 05:54:50
问题 C++ I'm trying to implement a function wrapper via a (function object) class (variadic) template. The class has as its only data member a function pointer that is initialized by or assigned the function pointer it is wrapping. The parametrized constructor takes a function pointer and initializes the member by it. The operator() method takes argument(s) (or none) and calls the wrapped function with them. At least that's the idea. I get many errors, which I mark with comments. VC11 (with the

Const and non-const functors

你离开我真会死。 提交于 2019-12-10 03:14:43
问题 This seems like something that ought to be frequently asked and answered, but my search-fu has failed me. I'm writing a function which I want to accept a generic callable object of some kind (including bare function, hand-rolled functor object, bind , or std::function ) and then invoke it within the depths of an algorithm (ie. a lambda). The function is currently declared like this: template<typename T, typename F> size_t do_something(const T& a, const F& f) { T internal_value(a); // do some

Is There a Indirection Functor?

别说谁变了你拦得住时间么 提交于 2019-12-07 00:28:27
问题 I'm looking for a unary functor which will dereference it's argument and return the result. Of course I can write one, it just seemed like something should already exist. So given the code: const auto vals = { 0, 1, 2, 3 }; vector<const int*> test(size(vals), nullptr); iota(begin(test), end(test), data(vals)); transform(cbegin(test), cend(test), ostream_iterator<int>(cout, " "), [](const auto& i){ return *i; }); Live Example I was hoping that there was a functor that I could use instead of

Detecting function object (functor) and lambda traits

拜拜、爱过 提交于 2019-12-06 22:15:49
问题 How can I detect the return type and parameter types of nullary and unary function pointers, std::function objects, and functors (including lambdas)? Boost's function_traits and functional traits don't quite get me there out of the box, but I'm open to supplementing or replacing them. I could do something like this: namespace nsDetail { class Dummy { Dummy(); }; } template<class Fn> struct FnTraits; template<class R> struct FnTraits<R(*)()> { typedef nsDetail::Dummy ParamType; typedef R

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

懵懂的女人 提交于 2019-12-05 09:34:16
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_lt_10() { 22 return self.get_filtered_list(&{self.filter_lt_10}); 23 } 24 25 # private 26 method get

Is There a Indirection Functor?

无人久伴 提交于 2019-12-05 05:47:50
I'm looking for a unary functor which will dereference it's argument and return the result. Of course I can write one, it just seemed like something should already exist. So given the code: const auto vals = { 0, 1, 2, 3 }; vector<const int*> test(size(vals), nullptr); iota(begin(test), end(test), data(vals)); transform(cbegin(test), cend(test), ostream_iterator<int>(cout, " "), [](const auto& i){ return *i; }); Live Example I was hoping that there was a functor that I could use instead of the lambda. Does such a thing exist, or do I need to just use the lambda? Assuming that by "functor" you

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

我的未来我决定 提交于 2019-12-05 04:40:29
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::result_type> particle; particle = fun(); return particle; } The argument type should probably be funcT

Recursively call a function object

▼魔方 西西 提交于 2019-12-05 04:10:49
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 ?? ? #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. You can either use the name of the overloaded operator: operator()(n-1); or invoke the operator on the current object: (*this)(n-1); As DyP mentioned, you can call (*this)(n-1) .

Detecting function object (functor) and lambda traits

北慕城南 提交于 2019-12-05 03:38:06
How can I detect the return type and parameter types of nullary and unary function pointers, std::function objects, and functors (including lambdas)? Boost's function_traits and functional traits don't quite get me there out of the box, but I'm open to supplementing or replacing them. I could do something like this: namespace nsDetail { class Dummy { Dummy(); }; } template<class Fn> struct FnTraits; template<class R> struct FnTraits<R(*)()> { typedef nsDetail::Dummy ParamType; typedef R ReturnType; typedef R Signature(); }; template<class R, class P> struct FnTraits<R(*)(P)> { typedef P