stdbind

std::async using an rvalue reference bound to a lambda

不羁岁月 提交于 2019-12-01 15:07:41
问题 I'm trying to bind an rvalue reference to a lambda using std::bind , but I have issues when I throw that into a std::async call: (source) auto lambda = [] (std::string&& message) { std::cout << message << std::endl; }; auto bound = std::bind(lambda, std::string{"hello world"}); auto future = std::async(bound); // Compiler error here future.get() This issues a compiler error I'm not really sure how to interpret: error: no type named 'type' in 'class std::result_of(std::basic_string)>&()>' What

std::bind()-ing a base protected member function from a derived class's member function

落爺英雄遲暮 提交于 2019-12-01 14:21:21
问题 I want to bind() to my base class's version of a function from the derived class. The function is marked protected in the base. When I do so, the code compiles happily in Clang (Apple LLVM Compiler 4.1) but gives an error in both g++ 4.7.2 and in Visual Studio 2010. The error is along the lines of: "'Base::foo' : cannot access protected member." The implication is that the context for the reference is actually within bind() , where of course the function is seen as protected. But shouldn't

Different overloads with std::function parameters is ambiguous with bind (sometimes)

て烟熏妆下的殇ゞ 提交于 2019-12-01 00:12:33
I have two overloads of a function foo which take different std::function s which results in an ambiguity issue for the latter when used with the result of a std::bind . I don't understand why only this is ambiguous. void foo(std::function<void(int)>) {} void foo(std::function<int()>) {} void take_int(int) { } int ret_int() { return 0; } When using int() with a bind function I get an ambiguity error foo(std::bind(ret_int)); // ERROR With the gcc-5.1 error (and similar with clang) error: call to 'foo' is ambiguous foo(std::bind(ret_int)); ^~~ note: candidate function void foo(std::function<void

What's the best way to wrap a C callback with a C++11 interface?

寵の児 提交于 2019-11-30 17:45:30
Let's say this is a C function to be wrapped: void foo(int(__stdcall *callback)()); The two main pitfalls with C function pointer callbacks are: Not being able to store bind expressions Not being able to store capturing lambdas I would like to know the best way to wrap functions like these to do so. The first is particularly useful for a member function callback, and the second for an inline definition that uses surrounding variables, but those are not the only uses. The other property of these particular function pointers is that they need to use the __stdcall calling convention. This, to my

std::bind and perfect forwarding

大兔子大兔子 提交于 2019-11-30 17:11:11
问题 The following code does not compile: #include <functional> template<class ...Args> void invoke(Args&&... args) { } template<class ...Args> void bind_and_forward(Args&&... args) { auto binder = std::bind(&invoke<Args...>, std::forward<Args>(args)...); binder(); } int main() { int a = 1; bind_and_forward(a, 2); } If I understand correctly, the reason is as follows: std::bind copies its arguments, and when the binder 's operator() is called, it passes all the bound arguments as lvalues - even

std::bind lose reference when delivered as rvalue reference

耗尽温柔 提交于 2019-11-30 11:15:44
I have the following code: #include <stdio.h> #include <functional> template <typename T> auto callback(T&& func) ->decltype(func()) { return func(); } double test(double& value) { value=value+1.0; return value; } int main(void) { double t=1.0; printf("%f\n",t); test(t); printf("%f\n",t); callback(std::bind(test,t)); printf("%f\n",t); } And it outputs 1.000000 2.000000 2.000000 Which implies the callback function got a copy of t instead of a reference to t . I am wondering what happened, since for std::bind it should be perfect-forwarding. std::bind uses value semantics by default. It's a sane

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() :

std::bind lose reference when delivered as rvalue reference

余生颓废 提交于 2019-11-29 16:29:31
问题 I have the following code: #include <stdio.h> #include <functional> template <typename T> auto callback(T&& func) ->decltype(func()) { return func(); } double test(double& value) { value=value+1.0; return value; } int main(void) { double t=1.0; printf("%f\n",t); test(t); printf("%f\n",t); callback(std::bind(test,t)); printf("%f\n",t); } And it outputs 1.000000 2.000000 2.000000 Which implies the callback function got a copy of t instead of a reference to t . I am wondering what happened,

use std::bind with overloaded functions

陌路散爱 提交于 2019-11-29 07:03:31
I cannot find out how to bind a parameter to an overloaded function using std::bind . Somehow std::bind cannot deduce the overloaded type (for its template parameters). If I do not overload the function everything works. Code below: #include <iostream> #include <functional> #include <cmath> using namespace std; using namespace std::placeholders; double f(double x) { return x; } // std::bind works if this overloaded is commented out float f(float x) { return x; } // want to bind to `f(2)`, for the double(double) version int main() { // none of the lines below compile: // auto f_binder = std:

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

时光怂恿深爱的人放手 提交于 2019-11-29 01:36:22
问题 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() {} }; //-