Questions tagged [boost-function]

A Boost C++ library that provide a family of class templates that are function object wrappers, similar to generalized callbacks.

The Boost.Function library defines boost::function, a class template that is callable like a function, and wraps another callable type and forwards calls to it.

Boost.Function was proposed for standardisation and was included in the Technical Report on C++ Library Extensions () as std::tr1::function and included in the C++ 2011 standard as std::function ().

160 questions
83
votes
1 answer

how boost::function and boost::bind work

I dislike having magic boxes scattered all over my code...how exactly do these two classes work to allow basically any function to be mapped to a function object even if the function<> has a completely different parameter set to the one im passing…
Fire Lancer
  • 27,564
  • 26
  • 109
  • 168
80
votes
1 answer

How to use boost bind with a member function

The following code causes cl.exe to crash (MS VS2005). I am trying to use boost bind to create a function to a calls a method of myclass: #include "stdafx.h" #include #include #include class…
hamishmcn
  • 7,333
  • 10
  • 38
  • 45
25
votes
1 answer

Is using boost::bind to pass more arguments than expected safe?

Using boost-bind, the resulting boost-function may receive more arguments than the bound object expects. Conceptually: int func() { return 42; } boost::function boundFunc = boost::bind(&func); int answer = boundFunc(1,2,3); In…
Jeff Benshetler
  • 600
  • 5
  • 12
19
votes
2 answers

Default value for boost::function argument?

I've got a function that I want to take an optional boost::function argument as a callback for reporting an error condition. Is there some special value I can use a the default value to make it optional? For example, with a regular function pointer…
gct
  • 12,874
  • 12
  • 59
  • 91
14
votes
1 answer

When to use std::function instead of inheritance?

In some cases std::function can replace inheritance. The following two code snippets are very similar (about the same costs when calling the function, almost the same usage in signatures and in most cases std::function does not require us to make an…
Markus Mayr
  • 3,568
  • 16
  • 38
13
votes
3 answers

Performance of std::function compared to raw function pointer and void* this?

Library code: class Resource { public: typedef void (*func_sig)(int, char, double, void*); //Registration registerCallback(void* app_obj, func_sig func) { _app_obj = app_obj; _func = func; } //Calling when the time…
balki
  • 22,482
  • 26
  • 85
  • 135
12
votes
1 answer

Why is "boost::function = boost::bind(...)" creating 13 temporaries?

I have some pretty basic test code. I have a class that just logs all operations on it. I bound it to a boost::function object like this: void Function(const Foo&) { printf("Function invoked\n"); } // ... …
David Schwartz
  • 166,415
  • 16
  • 184
  • 259
11
votes
2 answers

How to use boost::bind with non-copyable params, for example boost::promise?

Some C++ objects have no copy constructor, but have move constructor. For example, boost::promise. How can I bind those objects using their move constructors ? #include void fullfil_1(boost::promise& prom, int x) { …
user222202
  • 523
  • 4
  • 13
11
votes
2 answers

Boost::Bind and virtual function overloads: why do they work?

I wrote some code and got scared that it will not work - so I wrote a prototype: #include #include #include class base { private: boost::function action; protected: virtual void…
DuckQueen
  • 48
  • 6
  • 46
  • 109
9
votes
1 answer

Can tr1::function swallow return values?

The boost::function FAQ item 3 specifically addresses the scenario I am interested in: Why are there workarounds for void returns? C++ allows them! Void returns are permitted by the C++ standard, as in this code snippet: void f(); void g() {…
Chris
  • 103
  • 2
9
votes
3 answers

" does not provide a call operator" error when trying to wrap function return value

I'm trying to write a function that will take a functor as an argument, invoke the functor and then return its return value wrapped in a boost::shared_ptr. The following refuses to compile and I'm all out of ideas. I get "std::vector< std::string >…
Lucas
  • 6,168
  • 8
  • 33
  • 48
8
votes
1 answer

C++ weird syntax spotted in Boost template parameters

I was having a look at the "Function" class documentation in Boost, and stumbled across this: boost::function f; I must admit this syntax is highly confusing for me. How can this be legal C++ ? Is there any trick under the…
Dinaiz
  • 2,123
  • 3
  • 21
  • 29
8
votes
5 answers

Getting return value from a boost::threaded member function?

I have a worker class like the one below: class Worker{ public: int Do(){ int ret = 100; // do stuff return ret; } } It's intended to be executed with boost::thread and boost::bind, like: Worker worker; boost::function
He Shiming
  • 5,272
  • 3
  • 32
  • 59
7
votes
3 answers

Class member function as callback using boost::bind and boost::function

I'm working through setting up a member function as a callback for a C-library that I'm using. The C-library sets up callbacks like this: typedef int (*functionPointer_t)(myType1_t*, myType2_t*, myType3_t*); setCallback(param1, param2,…
jdt141
  • 4,655
  • 6
  • 32
  • 35
7
votes
2 answers

boost::function run-time performance

I'm in the process of implementing a platform independent wrapper for dynamically loaded libraries. Of course when I load functions from the libraries I need to store them as pointers for future use. I thought of using boost::function's for that…
Kornel Kisielewicz
  • 51,225
  • 12
  • 100
  • 147
1
2 3
10 11