std-function

What's the point of std::function constructor with custom allocator but no other args?

风格不统一 提交于 2019-12-05 02:13:15
I'm playing around with std::function and custom allocators but its not behaving as I expected when I don't provide the function with an initial functor. When I provide a custom allocator to the constructor but no initial functor, the allocator is never used or so it seems. This is my code. //Simple functor class that is big to force allocations struct Functor128 { Functor128() {} char someBytes[128]; void operator()(int something) { cout << "Functor128 Called with value " << something << endl; } }; int main(int argc, char* argv[]) { Allocator<char, 1> myAllocator1; Allocator<char, 2>

How to get this pointer from std::function?

我与影子孤独终老i 提交于 2019-12-05 00:02:39
问题 Since std::function can hold member functions, so it must store a pointer to the object instance somewhere. How can I fetch the this pointer from a std::function that holds a member function? 回答1: An object of type std::function holds a callable object . A pointer to member function is a kind of callable object; it can be called with an argument of the appropriate class type, plus any additional arguments that it needs. For example: struct S { void f(int); }; std::function<void(S, int)> g(&S:

Odd return behavior with std::function created from lambda (C++)

怎甘沉沦 提交于 2019-12-04 23:40:44
I'm having trouble with std::functions created from lambdas if the function returns a reference but the return type isn't explicitly called out as a reference. It seems that the std::function is created fine with no warnings, but upon calling it, a value is returned when a reference is expected, causing things to blow up. Here's a very contrived example: #include <iostream> #include <vector> #include <functional> int main(){ std::vector<int> v; v.push_back(123); std::function<const std::vector<int>&(const std::vector<int>&)> callback = [](const std::vector<int> &in){return in;}; std::cout <<

C++ Dynamically load arbitrary function from DLL into std::function

心已入冬 提交于 2019-12-04 20:35:37
问题 How can I load an arbitrary dynamic-link library (dll) function into a std::function object using a single function? For example I would like to compile two functions into a dll: // test.dll int plusFive(int value) { return value + 5; } void printHello() { std::cout << "Hello!" << std::endl; } And load them both at runtime using a single function like this: // main.cc #include <functional> int main() { std::function<int(int)> func1(loadDllFunc("test.dll", "plusFive")); std::function<void()>

Why std::function does not implicitly convert to bool in C++11? [duplicate]

不想你离开。 提交于 2019-12-04 14:54:53
This question already has an answer here: No viable conversion from std::function to bool 1 answer Consider the following code. #include <functional> int main(void) { std::function<void()> f1; if (f1) { /* ok */ ... } bool b = f1; /* compile-error */ bool B = !f1; /* ok */ ... } std::function<> converts implicitly to bool in some circumstances but not in all of them. Assigning it to a bool -variable does not work, whereas the result of an operation or using it in an if() -statement is OK. Why is that so? It seems we have to do an boolean-operation on it, then the conversion works. What I did

Replacing std::function from within itself (by move-assignment to *this?)

给你一囗甜甜゛ 提交于 2019-12-04 10:28:44
Is it possible to replace one std::function from within itself with another std::function ? The following code does not compile: #include <iostream> #include <functional> int main() { std::function<void()> func = []() { std::cout << "a\n"; *this = std::move([]() { std::cout << "b\n"; }); }; func(); func(); func(); } Can it be modified to compile? The error message right now is: 'this' was not captured for this lambda function - which I completely understand. I don't know, however, how I could capture func 's this -pointer. I guess, it is not even a std::function inside the lambda, yet?! How

How do I store a vector of std::bind without a specific case for the template?

那年仲夏 提交于 2019-12-04 05:46:08
After going though a question on std::bind , I was wondering if it was possible to hold a vector of functions created by std::bind so I can avoid using std::function and its heavyweight wrapping. #include <iostream> #include <functional> #include <typeinfo> #include <vector> int add(int a, int b) {return a + b;} int main() { //I believe this here is just a special type of bound function. auto add2 = std::bind(add, std::placeholders::_1, 2); auto add3 = std::bind(add, std::placeholders::_1, 3); //Yup. std::cout << typeid(add2).name() << std::endl; //Here's the type of the second function std:

How to pass std::function with different parameters to same function

心已入冬 提交于 2019-12-04 03:27:46
I have three functions I'm looking to merge together. Each one takes an std::function as the first parameter, then executes it within a try / catch block. The issue is, there are three different types of functions. Functions with no parameters, those with one integer parameter, and those with two integer parameters. The ones with integer parameters also have their corresponding parameters passed through the original function. As one can see, each of the functions are nearly identical, so it would be nice if I could merge them all together. However, I'm unsure of anyway to setup a parameter

g++: std::function initialized with closure type always uses heap allocation?

*爱你&永不变心* 提交于 2019-12-04 00:32:41
问题 Some sources on the Internets (specifically this one) says that std::function use small-closure optimizations, e.g. it do not allocate heap if closure size is lower than some amount of data (link above indicates 16 bytes for gcc) So I went digging through g++ headers Looks like whether or not such optimization is applied is decided by this block of code in "functional" header (g++ 4.6.3) static void _M_init_functor(_Any_data& __functor, _Functor&& __f) { _M_init_functor(__functor, std::move(_

Create a std::function type with limited arguments

扶醉桌前 提交于 2019-12-03 14:52:15
Given the type of a callable function C , I want to get at compile time a std::function ; the type of which: has the same return type of function C the argument types are the first N argument types of function C This means that, for a given type void(int, char, double) and a given N , the type of the function is: N = 1 => result type: std::function<void(int)> N = 2 => result type: std::function<void(int, char)> N = 3 => result type: std::function<void(int, char, double)> N > 3 => compile time error Example: template<std::size_t N, typename R, typename... A> constexpr auto get() { return /*