std-function

Using `std::function<void(…)>` to call non-void function

纵然是瞬间 提交于 2019-11-26 12:19:39
问题 A while ago I used std::function pretty much like this: std::function<void(int)> func = [](int i) -> int { return i; }; Basically, I did this because I wanted to store different function objects in a std::function , but I didn\'t want to restrict the return types of these functions. Since this seemed to work, I went with it. But I\'m not convinced that it is safe to use, and I haven\'t been able to find any documentation on it. Does anyone know whether this usage is legitimate? Or more

how to avoid static member function when using gsl with c++

Deadly 提交于 2019-11-26 12:19:33
问题 I would like to use GSL within a c++ class without declaring member functions as static . The reason for this is because I don\'t know them too well and I\'m not sure about thread safety. From what I read, std::function might be a solution but I\'m not sure how to use it. My question comes down to how can I remove static in declaration of g ? #include<iostream> #include <functional> #include <stdlib.h> #include <gsl/gsl_math.h> #include <gsl/gsl_monte.h> #include <gsl/gsl_monte_plain.h>

std::function vs template

十年热恋 提交于 2019-11-26 03:01:46
问题 Thanks to C++11 we received the std::function family of functor wrappers. Unfortunately, I keep hearing only bad things about these new additions. The most popular is that they are horribly slow. I tested it and they truly suck in comparison with templates. #include <iostream> #include <functional> #include <string> #include <chrono> template <typename F> float calc1(F f) { return -1.0f * f(3.3f) + 666.0f; } float calc2(std::function<float(float)> f) { return -1.0f * f(3.3f) + 666.0f; } int

Isn&#39;t the template argument (the signature) of std::function part of its type?

六眼飞鱼酱① 提交于 2019-11-26 02:57:25
问题 Given the following code, what is the reason behind the ambiguity? Can I circumvent it or will I have to keep the (annoying) explicit casts? #include <functional> using namespace std; int a(const function<int ()>& f) { return f(); } int a(const function<int (int)>& f) { return f(0); } int x() { return 22; } int y(int) { return 44; } int main() { a(x); // Call is ambiguous. a(y); // Call is ambiguous. a((function<int ()>)x); // Works. a((function<int (int)>)y); // Works. return 0; }