std-function

How could I pass std::function as function pointer?

时光怂恿深爱的人放手 提交于 2019-12-01 10:58:19
I am trying to write a class template and internally it use a C function (implementation of BFGS optimization, provided by the R environment) with the following interface: void vmmin(int n, double *x, double *Fmin, optimfn fn, optimgr gr, ... , void *ex, ... ); where fn and gr are function pointers of type typedef double optimfn(int n, double *par, void *ex); and typedef void optimgr(int n, double *par, double *gr, void *ex); respectively. My C++ class template looks like this: template <typename T> class optim { public: // ... void minimize(T& func, arma::vec &dpar, void *ex) { std::function

A parallel for using std::thread?

最后都变了- 提交于 2019-12-01 05:43:27
I'm new with std::thread and I try to code a parallel_for . I coded the following thing: // parallel_for.cpp // compilation: g++ -O3 -std=c++0x parallel_for.cpp -o parallel_for -lpthread // execution: time ./parallel_for 100 50000000 // (100: number of threads, 50000000: vector size) #include <iostream> #include <iomanip> #include <cstdlib> #include <vector> #include <thread> #include <cmath> #include <algorithm> #include <numeric> #include <utility> // Parallel for template<typename Iterator, class Function> void parallel_for(const Iterator& first, const Iterator& last, Function&& f, const

C++14: Generic lambda with generic std::function as class member

*爱你&永不变心* 提交于 2019-12-01 04:21:47
Consider this pseudo-snippet: class SomeClass { public: SomeClass() { if(true) { fooCall = [](auto a){ cout << a.sayHello(); }; } else { fooCall = [](auto b){ cout << b.sayHello(); }; } } private: template<typename T> std::function<void(T)> fooCall; }; What I want is a class member fooCall which stores a generic lambda, which in turn is assigned in the constructor. The compiler complains that fooCall cannot be a templated data member. Is there any simple solution on how i can store generic lambdas in a class? There is no way you'll be able to choose between two generic lambdas at run-time, as

A parallel for using std::thread?

ぃ、小莉子 提交于 2019-12-01 03:04:19
问题 I'm new with std::thread and I try to code a parallel_for . I coded the following thing: // parallel_for.cpp // compilation: g++ -O3 -std=c++0x parallel_for.cpp -o parallel_for -lpthread // execution: time ./parallel_for 100 50000000 // (100: number of threads, 50000000: vector size) #include <iostream> #include <iomanip> #include <cstdlib> #include <vector> #include <thread> #include <cmath> #include <algorithm> #include <numeric> #include <utility> // Parallel for template<typename Iterator

Is it safe to change a function pointer (std::function) inside a called function?

元气小坏坏 提交于 2019-12-01 02:41:19
I have a std::function pointing to a function. Inside this function I change the pointer to another function. std::function<void()> fun; void foo() { std::cout << "foo\n"; } void bar() { std::cout << "bar\n"; fun = foo; } int main() { fun = bar; fun(); fun(); } I can't see any problem and it works just fine (see here ), however I'm not sure if this is legal to do so. Is there anything I am missing? Maybe in the c++ standard draft (I checked quickly but didn't see anything so far). This is legal with function pointers. When you assign or construct a std::function with a target, it creates a

C++14: Generic lambda with generic std::function as class member

依然范特西╮ 提交于 2019-12-01 01:17:31
问题 Consider this pseudo-snippet: class SomeClass { public: SomeClass() { if(true) { fooCall = [](auto a){ cout << a.sayHello(); }; } else { fooCall = [](auto b){ cout << b.sayHello(); }; } } private: template<typename T> std::function<void(T)> fooCall; }; What I want is a class member fooCall which stores a generic lambda, which in turn is assigned in the constructor. The compiler complains that fooCall cannot be a templated data member. Is there any simple solution on how i can store generic

Different overloads with std::function parameters is ambiguous with bind (sometimes)

て烟熏妆下的殇ゞ 提交于 2019-12-01 00:12:33
I have two overloads of a function foo which take different std::function s which results in an ambiguity issue for the latter when used with the result of a std::bind . I don't understand why only this is ambiguous. void foo(std::function<void(int)>) {} void foo(std::function<int()>) {} void take_int(int) { } int ret_int() { return 0; } When using int() with a bind function I get an ambiguity error foo(std::bind(ret_int)); // ERROR With the gcc-5.1 error (and similar with clang) error: call to 'foo' is ambiguous foo(std::bind(ret_int)); ^~~ note: candidate function void foo(std::function<void

Is it safe to change a function pointer (std::function) inside a called function?

孤街醉人 提交于 2019-11-30 22:23:24
问题 I have a std::function pointing to a function. Inside this function I change the pointer to another function. std::function<void()> fun; void foo() { std::cout << "foo\n"; } void bar() { std::cout << "bar\n"; fun = foo; } int main() { fun = bar; fun(); fun(); } I can't see any problem and it works just fine (see here), however I'm not sure if this is legal to do so. Is there anything I am missing? Maybe in the c++ standard draft (I checked quickly but didn't see anything so far). 回答1: This is

Template type deduction with std::function

六眼飞鱼酱① 提交于 2019-11-30 19:43:44
I have discovered the following behaviour with std::function and type deduction, which was unexpected for me: #include <functional> template <typename T> void stdfunc_test(std::function<T(T)> func) {}; int test_func(int arg) { return arg + 2; } int main() { stdfunc_test([](int _) {return _ + 2;}); stdfunc_test(test_func); } Both lines in main result in error: no instance of function template "stdfunc_test" matches the argument list When attempting to compile in Visual Studio 2015. Why doesn't the type deduction deduct template type from the function type, and is there a workaround for it? No

How should I define a std::function variable with default arguments?

自闭症网瘾萝莉.ら 提交于 2019-11-30 09:14:59
To set a std::function variable to a lambda function with default argument I can use auto as in: auto foo = [](int x = 10){cout << x << endl;}; foo(); This will print 10. But I want the foo variable to reside in a struct. In a struct I cannot use auto . struct Bar { auto foo = [](int x = 10}(cout << x << endl}; //error: non-static data member declared ‘auto’ }; Bar bar; bar.foo(); Replacing auto with std::function struct Bar { std::function<void(int x = 10)> foo = [](int x = 10}(cout << x << endl}; //error: default arguments are only permitted for function parameters }; Bar bar; bar.foo(); or