std-function

Deleting a std::function object within itself

*爱你&永不变心* 提交于 2019-12-28 20:36:09
问题 Is this well defined behavior? #include <functional> void foo() { auto f = new std::function<void()>; *f = [f]() { delete f; }; (*f)(); f = nullptr; } int main() { foo(); } Using the most recent g++, if I do this within a template it causes invalid reads while running under valgrind, otherwise it works fine. Why? Is this a bug in g++? #include <functional> template<std::size_t> void foo() { auto f = new std::function<void()>; *f = [f]() { delete f; }; (*f)(); f = nullptr; } int main() { foo<0

c++11: How to write a wrapper function to make `std::function` objects

别来无恙 提交于 2019-12-28 12:34:55
问题 I am trying to write a wrapper make_function , which like std::make_pair can create a std::function object out of suitable callable objects. Just like make_pair , for a function pointer foo , auto f0 = make_function(foo); creates a std::function function object f0 of the right type signature. Just to clarify, I don't mind occasionally giving type parameters to make_function in case it is difficult (or impossible) to deduce the type entirely from the parameters. What I came up with so far

c++11: How to write a wrapper function to make `std::function` objects

江枫思渺然 提交于 2019-12-28 12:34:36
问题 I am trying to write a wrapper make_function , which like std::make_pair can create a std::function object out of suitable callable objects. Just like make_pair , for a function pointer foo , auto f0 = make_function(foo); creates a std::function function object f0 of the right type signature. Just to clarify, I don't mind occasionally giving type parameters to make_function in case it is difficult (or impossible) to deduce the type entirely from the parameters. What I came up with so far

Vector of std::function with different signatures

偶尔善良 提交于 2019-12-28 05:45:06
问题 I have a number of callback functions with different signatures. Ideally, I would like to put these in a vector and call the appropriate one depending on certain conditions. e.g. void func1(const std::string& value); void func2(const std::string& value, int min, int max); const std::vector<std::function<void(std::string)>> functions { func1, func2, }; I realise the above isn't possible, but I wonder if there are any alternatives I should consider. I haven't been able to find any yet, and I've

casting void* to std::function

陌路散爱 提交于 2019-12-24 11:15:35
问题 I have an issue. I'm trying to convert a void* to std::function. This is just a simple example, any suggestions will be appreciated #.h file class Example { public: Example(); int foo(void* hi); int fooFunc(std::function<int(int, int)> const& arg, int x, int y) { foo(arg.target<void*>(), x, y); return 2; } }; #.cpp file Example::Example() { } int Example::foo(void * func, int x, int y) { //cast back to std::function func(x, y); std::cout << "running in foo: " << a << "\n"; return a; } Every

Deducing return and parameter type from std::function passed as a template function argument?

孤街醉人 提交于 2019-12-24 03:53:17
问题 I've been looking around SO for a while now but cannot find quite the answer that I am looking for - this question is probably the closest to what I am thinking. In a sentence: is it possible to declare a template function that takes a parameter of an std::function and deduce template parameters for both the return type and the parameter types of the function? Example: //this works to pass the std::function in template<class T> void doSomething(std::function<T> f) { f(); } //this is more what

Why should bind() be deprecated?

十年热恋 提交于 2019-12-23 12:33:12
问题 Reading the proposal for C++17 about removing some deprecated, old and unused parts of the standard (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4190.htm) i find section D.9 a bit strange: D.9 "Binders" [depr.lib.binders] This defines bind1st()/bind2nd(), which were strictly superseded by bind(). (In the future, I'll argue that bind() itself has been superseded by lambdas and especially generic lambdas, so bind() should be deprecated, but that isn't part of this proposal.) What I

is std::function heavier than auto storing lambda function

守給你的承諾、 提交于 2019-12-23 10:02:41
问题 I heard that cost of std::function is heavier than auto to deal with a lambda function. effective modern c++ item5. What I want is to clarify the mechanism why std::function use more memory than auto with some sample code. Could somebody help me? edit class Widget { public: Widget(int i) : i_(i) {} bool operator<(const Widget& o) { return o.value() > i_; } int value() const { return i_; }; private: int i_; int dummy_[1024]; }; int main() { // performance difference between auto and std:

Strange behavior with std::function

空扰寡人 提交于 2019-12-22 08:39:20
问题 I'm using the standard function wrapper from the C++11 library, and I am seeing some strange behavior with its boolean operator. If I create a std::function object the boolean operator returns false. This is still true if I assign nullptr to the object and check again. The problem appears when I assign it a void pointer which I have cast into a function pointer. Consider the following program: #include <functional> #include <iostream> void* Test() { return nullptr; } int main(int argc, char*

Binding a std::function to the same function of a different object instance

一世执手 提交于 2019-12-22 07:56:43
问题 Is it possible to rebind a std::function to point to the same function but with a different object instance? Say if I have an object that has a std::function that is bound to another function, but if that object was copied to another instance, I'd like to rebind the std::function to that new instance instead of the old instance. #include "stdafx.h" #include <iostream> #include <functional> class EventHandler { public: int Num; std::function<int()> OnEvent; EventHandler (int inNum) { Num =