std-function

Comparing std::functions for equality?

倖福魔咒の 提交于 2019-11-27 02:03:26
问题 How can I compare two C++11 std::function s with operator== , and return true if both of said function s refer to the same function pointer? 回答1: operator== for std::function compares a std::function with a null pointer, as far as I can tell the standard does not provide any details as to why. Although, this boost FAQ entry, Why can't I compare boost::function objects with operator== or operator!=? provides a rationale and as far as I can tell should be applicable to std::function as well.

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

主宰稳场 提交于 2019-11-27 02:01:00
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> #include <gsl/gsl_monte_miser.h> #include <gsl/gsl_monte_vegas.h> using namespace std; class A { public: static

Deduce template argument from std::function call signature

若如初见. 提交于 2019-11-26 23:10:56
Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun(); } Why isn't it possible for the compiler to deduce ReturnT from the passed call signature? bool bar() { /* ... */ } foo<bool>(bar); // works foo(bar); // error: no matching function call std::function<bool()> bar; foo(bar); // works just fine C++ can't deduce the return type from your function bar because it would have to know the type before it could find all the constructors that take your function pointer. For example, who's to say that std::function<std::string()>

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

我怕爱的太早我们不能终老 提交于 2019-11-26 23:03:18
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 generally, what the rules are for the object which can safely be assigned to a std::function ? Edit For

std::function fails to distinguish overloaded functions

天大地大妈咪最大 提交于 2019-11-26 17:46:16
问题 I am trying to understand why std::function is not able to distinguish between overloaded functions. #include <functional> void add(int,int){} class A {}; void add (A, A){} int main(){ std::function <void(int, int)> func = add; } In the code shown above, function<void(int, int)> can match only one of these functions and yet it fails. Why is this so? I know I can work around this by using a lambda or a function pointer to the actual function and then storing the function pointer in function.

How to directly bind a member function to an std::function in Visual Studio 11?

和自甴很熟 提交于 2019-11-26 16:49:48
问题 I can easily bind member functions to a std::function by wrapping them with a lambda expression with capture clause. class Class { Class() { Register([=](int n){ Function(n); }); } void Register(std::function<void(int)> Callback) { } void Function(int Number) { } }; But I want to bind them directly, something like the following. // ... Register(&Class::Function); // ... I think according to the C++11 standard, this should be supported. However, in Visual Studio 11 I get these compiler errors.

Is it illegal to invoke a std::function<void(Args…)> under the standard?

吃可爱长大的小学妹 提交于 2019-11-26 16:23:00
问题 All quotes are from N3797. 4/3 [conv] An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t This implies no expression can be implicitly converted to void , as void t=e is illegal for all expressions e . This is even true if e is an expression of type void , such as void(3) . So an expression of type void cannot be implicitly converted to void . Which leads us to: 20.9.2/2 Requirements [func.require

std::function and std::bind: what are they, and when should they be used?

 ̄綄美尐妖づ 提交于 2019-11-26 15:01:36
问题 I know what functors are and when to use them with std algorithms, but I haven't understood what Stroustrup says about them in the C++11 FAQ. Can anyone explain what std::bind and std::function are, when they should be used, and give some examples for newbies? 回答1: std::bind is for partial function application. That is, suppose you have a function object f which takes 3 arguments: f(a,b,c); You want a new function object which only takes two arguments, defined as: g(a,b) := f(a, 4, b); g is a

std::function vs template

独自空忆成欢 提交于 2019-11-26 12:38:35
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 main() { using namespace std::chrono; const auto tp1 = system_clock::now(); for (int i = 0; i < 1e8; ++i

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

∥☆過路亽.° 提交于 2019-11-26 12:25:13
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; } Interestingly, if I comment out the a() function with the function<int ()> parameter and call a(x) in my main,