functor

passing member-function as argument to function-template

余生长醉 提交于 2020-01-14 13:29:10
问题 Consider three ways to implement a routine in c++: through functors, member functions, and non-member functions. For example, #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class FOO { public: void operator() (string word) // first: functor { cout << word << endl; } void m_function(string word) // second: member-function { cout << word << endl; } } FUNCTOR; void function(string word) // third: non-member function { cout << word << endl; } Now

Parameter to use std::greater or std::less as argument

孤街醉人 提交于 2020-01-13 07:55:09
问题 I would like to make a function with a parameter that accepts either std::greater<int> or std::less<int> as the argument. I'm stuck on the syntax for the parameter, though. This is the format I tried: myFunction(int a, int b, bool *comp(int, int)) { … } … std::greater<int> bigger; myFunction(2, 3, bigger); That doesn't work, though, and I suspect the third parameter is just completely wrong. What should it actually be? cannot convert std::greater<int> to bool* (*)(int, int) 回答1: Functions

Multiple predicates specified during runtime

别说谁变了你拦得住时间么 提交于 2020-01-13 04:48:12
问题 There are operator classes in STL like less, equal_to, greater_equal etc. How to easily combine them to use with for example remove_if function? For example I want to remove in vector elements which are greater than 0 AND less than 3 AND not equal to 2, then it would be something like: remove_if (v.begin(), v.end(), bind2nd(greater<int>(),0) + bind2nd(less<int>(),3) + not1(bind2nd(equal_to<int>(), 2))); User during running of program can specify filtering options for example he can write:

What is the general case of QuickCheck's promote function?

Deadly 提交于 2020-01-11 04:50:06
问题 What is the general term for a functor with a structure resembling QuickCheck's promote function, i.e., a function of the form: promote :: (a -> f b) -> f (a -> b) (this is the inverse of flip $ fmap (flip ($)) :: f (a -> b) -> (a -> f b) ). Are there even any functors with such an operation, other than (->) r and Id ? (I'm sure there must be). Googling 'quickcheck promote' only turned up the QuickCheck documentation, which doesn't give promote in any more general context AFAICS; searching SO

Function pointers in embedded systems, are they useful?

↘锁芯ラ 提交于 2020-01-10 01:35:08
问题 In an interview they asked me if using function pointers would be beneficial (in terms of speed) when writing code for embedded systems? I had no idea on embedded system so could not answer the question. Just a cloudy or vague answer. So what are the real benefits? Speed, readability, maintenance,cost? 回答1: I think perhaps Viren Shakya's answer misses the point that the interviewer was trying to elicit. In some constructs the use of a function pointer may speed up execution. For example, if

Why override operator()?

瘦欲@ 提交于 2020-01-08 11:41:19
问题 In the Boost Signals library, they are overloading the () operator. Is this a convention in C++? For callbacks, etc.? I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me. Any insight as to the reason for this overload? 回答1: One of the primary goal when overloading operator() is to create a functor. A functor acts just like a function, but it has the advantages that it is stateful, meaning it

Why override operator()?

不打扰是莪最后的温柔 提交于 2020-01-08 11:41:09
问题 In the Boost Signals library, they are overloading the () operator. Is this a convention in C++? For callbacks, etc.? I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me. Any insight as to the reason for this overload? 回答1: One of the primary goal when overloading operator() is to create a functor. A functor acts just like a function, but it has the advantages that it is stateful, meaning it

How do I Write a Custom Deleter to Handle Forward Declared Types

删除回忆录丶 提交于 2020-01-06 08:22:05
问题 Some older versions of visual-studio required a complete definition of the type for default_deleter to be defined. (I know this was the case in Visual Studio 2012 but I'd give bonus points to anyone who could tell me which version remedied this.) To work around this I can write a custom_deleter function like so: template <typename T> void custom_deleter(T* param) { delete param; } And declare a smart pointer on a forward declared type like so: unique_ptr<Foo, function<void(Foo*)>> pFoo ,

Extracting nested monadic result: m (m a) -> m a

百般思念 提交于 2020-01-06 01:45:20
问题 I have a function parseArgs :: [String] -> StdGen -> IO () which selects the function to run. The main looks like main = parseArgs <$> getArgs <*> getStdGen >>= id The problem I have, parseArgs <$> getArgs <*> getStdGen is of type IO (IO ()) , which I extract using (>>= id) which is of type Monad m => m (m b) -> m b . Is there a way to avoid requiring the "extraction" of the value while having just a single line function? 回答1: The easiest way would be with join : main = join $ parseArgs <$>

Which algorithms require functors to be pure functions?

随声附和 提交于 2020-01-05 01:33:59
问题 Generally the standard requires functors to be pure functions because algorithms are allowed to copy their functors to their heart's content. However, there are some algorithms (e.g. find_if ) for which no sane implementation would be doing any form of functor copying. Can we rely on this? 回答1: I think what you're trying to ask is which functors need to be stateless due to being copied at arbitrary times. I can't think of any algorithms that require free functions to be used, but most/all