functor

How to assign functor to function pointer?

心不动则不痛 提交于 2019-12-23 05:41:58
问题 Generally, can I assign a function object to a function pointer? I want to do something like this: #include <iostream> class Foo { int num; public: Foo(int num_par) : num(num_par) {} void operator()(int multiplier) { std::cout << multiplier * num << std::endl; } }; int main() { typedef void(*bar)(int); Foo f(42); bar b = f; // MSVC error // ^ C2440: 'initializing' : cannot convert from 'Foo' to 'bar' b(2); // wanted to print 84 } If that's impossible, I’d like an alternative specifically for

How to assign functor to function pointer?

微笑、不失礼 提交于 2019-12-23 05:41:06
问题 Generally, can I assign a function object to a function pointer? I want to do something like this: #include <iostream> class Foo { int num; public: Foo(int num_par) : num(num_par) {} void operator()(int multiplier) { std::cout << multiplier * num << std::endl; } }; int main() { typedef void(*bar)(int); Foo f(42); bar b = f; // MSVC error // ^ C2440: 'initializing' : cannot convert from 'Foo' to 'bar' b(2); // wanted to print 84 } If that's impossible, I’d like an alternative specifically for

Is there a macro-based adapter to make a functor from a class?

穿精又带淫゛_ 提交于 2019-12-23 04:57:08
问题 Creating a functor requires an unnecessary boiler plate. The state has to be written 4 times! struct f{ double s; // 1st f(double state): s(state) {} // 2nd, 3rd and 4th double operator() (double x) { return x*s; } }; is there a library with a macro that would be just double functor(state)(x){ return x*state; } or something similar. BOOST_FOREACH is a macro adapter that works well. I'm looking for something similar. any suggestions on how to write one is appreciated too. ps. using struct for

How does wrapping a function pointer and function object work in generic code?

╄→гoц情女王★ 提交于 2019-12-23 04:34:17
问题 The following template definition template <typename Func, typename ReturnType, typename... Arguments> class Command { public: Command(Func f) : m_func(f) { } ReturnType operator()(Arguments... funcArgs) { return m_func(funcArgs...); } private: Func m_func; }; gives an error message with gcc 4.7.3 (error: field 'Command::m_func' invalidly declared function type) when instantiated with the following test code: void testFunction(int i, double d) { std::cout << "TestFunctor::operator()(" << i <<

Can you use a functor/functional programming to group a list in Java 7 (and count its elements per group)?

不打扰是莪最后的温柔 提交于 2019-12-23 04:24:38
问题 Can you group List<TypeEnum> types = new ArrayList(Arrays.asList(TypeEnum.A, TypeEnum.B, TypeEnum.A)); into a Map<TypeEnum, Integer> countPerType; , using functors (e.g. Google's Guava, Apache's Commons Functor) pre Java 8? I'm trying to get my head around functional programming, but am unsure if that sort of thing would actually be possible (as I'm not just mapping a collections value, but trying to aggregate)? In imperative style, I would have done something like this: public Map<TypeEnum,

How to force a functor to see an entire thrust::vector so that sorting is possible?

好久不见. 提交于 2019-12-23 02:46:13
问题 I'm new to CUDA and having a little trouble with functors. I am trying to input a thrust::vector of thrust::vectors into a functor. Currently I can enter a vector and do something to each element and return the modified vector using thrust::for_each, but if I were to want to sort a vector in a functor I would need to be able to input the whole vector at once so the functor can act on it as a whole. Is there a way to do this? The code below compiles, but does not return the vector sorted.

Passing a complex function variants as arguments

泄露秘密 提交于 2019-12-22 11:29:28
问题 Say I have the following template function: template <class T> void apply(const vector<complex<T> >& in, vector<T>& out, T (*f)(complex<T>)) { out.resize(in.size()); for(size_t i = 0; i < in.size(); ++i) out[i] = f(in[i]); } You can see, I just want to apply a function to a vector of complex data, and store the results into a vector of real data. I figure this should be good for a whole list of function: abs, norm, real, imag, etc. My problem is, how do I pass a function in? I have tried

Using non-const expression as template parameter

徘徊边缘 提交于 2019-12-22 10:50:54
问题 This is a follow up on How do I get the argument types of a function pointer in a variadic template class? I have this struct to access the arguments of a variadic template: template<typename T> struct function_traits; template<typename R, typename ...Args> struct function_traits<std::function<R(Args...)>> { static const size_t nargs = sizeof...(Args); typedef R result_type; template <size_t i> struct arg { typedef typename std::tuple_element<i, std::tuple<Args...>>::type type; }; }; And I

Can two functors be compared for equality?

让人想犯罪 __ 提交于 2019-12-22 06:36:25
问题 Is there a way for a method, which receives two functors as arguments, to find out if they are pointing to the same function? Specifically, having a struct like this: struct FSMAction { void action1() const { std::cout << "Action1 called." << std::endl; } void action2() const { std::cout << "Action2 called." << std::endl; } void action3() const { std::cout << "Action3 called." << std::endl; } private: // Maybe some object-specific stuff. }; And a method like this: bool actionsEqual( const std

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