std-function

Default function that just returns the passed value?

人盡茶涼 提交于 2019-11-30 08:49:42
As a lazy developer, I like to use this trick to specify a default function: template <class Type, unsigned int Size, class Function = std::less<Type> > void arrange(std::array<Type, Size> &x, Function&& f = Function()) { std::sort(std::begin(x), std::end(x), f); } But I have a problem in a very particular case, which is the following: template <class Type, unsigned int Size, class Function = /*SOMETHING 1*/> void index(std::array<Type, Size> &x, Function&& f = /*SOMETHING 2*/) { for (unsigned int i = 0; i < Size; ++i) { x[i] = f(i); } } In this case, I would like the default function to be

Collection of std::functions with different arguments

为君一笑 提交于 2019-11-30 08:37:01
问题 I am trying to write a simple dispatcher, the user code can attach callbacks to it. Each event has a known signature, and the user code will need to call dispatch with the right number and argument types. This is managed by the variadic arguments. But, freestandingInt is not accepted, as the vector is not of the right type. How to make it generic? Follows a minimal example void freestanding() { std::cout << "freestanding" << std::endl; } void freestandingInt(int iArg) { std::cout <<

Why do std::function instances have a default constructor?

♀尐吖头ヾ 提交于 2019-11-30 08:02:22
问题 This is probably a philosophical question, but I ran into the following problem: If you define an std::function, and you don't initialize it correctly, your application will crash, like this: typedef std::function<void(void)> MyFunctionType; MyFunctionType myFunction; myFunction(); If the function is passed as an argument, like this: void DoSomething (MyFunctionType myFunction) { myFunction(); } Then, of course, it also crashes. This means that I am forced to add checking code like this: void

Using std::function and std::bind to store callback and handle object deletion.

人走茶凉 提交于 2019-11-30 05:10:16
I want to implement a manager that stores callbacks to member functions of polymorphic classes using C++11. The issue is that I am not sure how to handle the case where the object that the member belongs to gets deleted or should be deleted and I want to make the interface as simple as possible. So I thought of the following: Store a std::weak_ptr to the object as well as a std::function to the member. The following seems to work: class MyBase { public: MyBase() {} virtual ~MyBase() {} }; //-------------------------------------------------- class MyClass : public MyBase { public: MyClass() :

Have the ideas behind the Fast Delegate (et al) been used to optimize std::function?

旧街凉风 提交于 2019-11-30 04:54:27
There have been proposals for C++ "delegates" which have lower overhead than boost::function : Member Function Pointers and the Fastest Possible C++ Delegates Fast C++ Delegate The Impossibly Fast C++ Delegates Have any of those ideas been used to implement std::function , resulting in better performance than boost::function ? Has anyone compared the performance of std::function vs boost::function ? I want to know this specifically for the GCC compiler and libstdc++ on Intel 64-bit architectures , but information on other compilers is welcome (such as Clang). In libstdc++'s std::function we

Template type deduction with std::function

假如想象 提交于 2019-11-30 03:57:17
问题 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

C++ How to Reference Templated Functions using std::bind / std::function

三世轮回 提交于 2019-11-30 03:29:19
If you have a templated class or a templated function, (or combination of the two), how do you bind that function, (preserving the template type parameter)? I was given some help about the basic syntax in a post below, to bind to functions with explicit template type parameters, but lose the ability to provide template type parameters in the process. Is it possible to get this to work so that it is still possible to provide template type parameters with future calls? Cleaned up this code a lot, but it obviously won't compile because I can't find the correct syntax, (are there any ways of doing

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

こ雲淡風輕ζ 提交于 2019-11-30 03:11:04
问题 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

Should std::function assignment ignore return type? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-11-29 13:56:48
This question already has an answer here: Using `std::function<void(…)>` to call non-void function 3 answers Is the code below valid C++ according to the C++11 or C++14 standard? #include <functional> int ReturnInt() { return 5; } int main( int argc, char **argv ) { std::function< void () > BoundType = ReturnInt; return 0; } The code compiles fine with the latest cygwin versions of gcc (4.8.3) and clang (4.3.2) but not with Visual Studio 2013, Visual Studio November 2013 CTP or the Visual Studio 14 preview. It also compiles on all platforms if std::function is changed to boost::function. I

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

我怕爱的太早我们不能终老 提交于 2019-11-29 13:03:58
问题 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 <<