Questions tagged [stdbind]

The C++11 function std::bind() fixes some or all arguments of a function object, returning another function object that takes fewer arguments.

std::bind performs Partial Function Application of arbitrary C++ function objects, fixing some or all arguments of a function object and returning another function object that takes fewer arguments.

The function is based on boost::bind() which originated in the library (see ) and an earlier version was included in as std::tr1::bind().

371 questions
13
votes
3 answers

Partial Binding of Function Arguments

Is there a way to partially bind the first/last n arguments of a callable object (e.g. function) without explicitly specifying the rest of the arguments? std::bind() seems to require that all the arguments are be bound, those that are to be left…
Adi Shavit
  • 16,743
  • 5
  • 67
  • 137
13
votes
2 answers

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

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…
OldPeculier
  • 11,049
  • 13
  • 50
  • 76
13
votes
2 answers

How to combine the use of std::bind with std::shared_ptr

I need to do something like this more than often: AsyncOperation * pAsyncOperation = new AsyncOperation(); auto bindOperation = std::bind(&AsyncOperation::operator(), std::ref(*pAsyncOperation)); std::thread thread(bindOperation…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
12
votes
3 answers

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

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…
chris
  • 60,560
  • 13
  • 143
  • 205
12
votes
2 answers

How do I `std::bind` a non-static class member to a Win32 callback function `WNDPROC`?

I'm trying to bind a non-static class member to a standard WNDPROC function. I know I can simply do this by making the class member static. But, as a C++11 STL learner, I'm very interested in doing it by using the tools under the
hkBattousai
  • 10,583
  • 18
  • 76
  • 124
12
votes
1 answer

Short way to std::bind member function to object instance, without binding parameters

I have a member function with several arguments. I'd like to bind it to a specific object instance and pass this to another function. I can do it with placeholders: // actualInstance is a MyClass* auto callback = bind(&MyClass::myFunction,…
lethal-guitar
  • 4,438
  • 1
  • 20
  • 40
11
votes
2 answers

use std::bind with overloaded functions

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…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
11
votes
4 answers

some practical uses of mem_fn & bind

Can someone recommend some cool practical uses of tr1's mem_fn and bind utilities? I don't need esoteric c++ for library development. just some application level coding which makes uses of these. any help will be very appreciated.
Fanatic23
  • 3,378
  • 2
  • 28
  • 51
10
votes
3 answers

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

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…
CJCombrink
  • 3,738
  • 1
  • 22
  • 38
9
votes
1 answer

Why is the copy ctor invoked twice when using std::bind?

I am playing around with std::function and std::bind to understand how arguments are copied around and if I can save some of the copy operations. I understand that when using std::bind, the arguments are passed by value and not reference (unless…
urnonav
  • 191
  • 7
9
votes
2 answers

Understanding std::function and std::bind

I was playing arround with std::function and std::bind and I noticed something unintuitive and I would like to understand it better. For example: void fun() { } void hun(std::string) { } int main() { function g = &fun; //This…
Alex
  • 600
  • 1
  • 5
  • 16
8
votes
1 answer

Difference between std::async and std::bind when wrapping rvalue reference lambda

Inspired by this comment about binding lambdas with rvalue reference parameters directly to std::async, binding an rvalue to a lambda through std::async compiles and executes as expected: (live example) auto lambda = [] (std::string&& message) { …
huu
  • 7,032
  • 2
  • 34
  • 49
8
votes
1 answer

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

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 =…
huu
  • 7,032
  • 2
  • 34
  • 49
8
votes
3 answers

C++11 random numbers and std::bind interact in unexpected way

I am using GCC 4.6.3 and was trying to generate random numbers with the following code: #include #include int main() { std::mt19937 rng_engine; printf("With bind\n"); for(int i = 0; i < 5; ++i) { …
patvarilly
  • 105
  • 1
  • 5
7
votes
0 answers

why I can't bind function with rvalue args like this?

#include #include #include #include using namespace std; template void do_task(Func &func, Args &&...args) { auto f = bind(func, forward(args)...); …
Xuan Lin
  • 81
  • 3
1
2
3
24 25