473,702 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returned type of boost::bind && Address of a tmp object

Hello,

in the following code I have a pointer (to function), say p,
of type
double (*)(double, double, void*)
and I try to fix the second argument of the function *p
to a given value (using boost::bind), but the compiler
complains, because of a type mismatch in an assignment
which I think should be legal:

/*
* bash-3.00$ g++ function.cpp
* function.cpp: In function `int main()':
* function.cpp:36 : warning: taking address of temporary
* function.cpp:36 : error: cannot convert
* `boost::_bi::bi nd_t<double,
* double (*)(double, double, void*),
* boost::_bi::lis t3<boost::arg<1 >,
*
boost::_bi::val ue<double>,
* boost::arg<2 >*'
* to `double (*)(double, void*)' in assignment
*/

Can you see why? The code is below.

There is also a warning which I don't like at all,
and remebers me that boost::bind( ... ) is a temporary
object and its address makes sense only in the current
scope (if I'm not wrong).
Since I have to pass this address as argument to another
function (later, in my real code), this can couse problems.

How can I build a "real" (I mean non-temporary) object
out of boost::bind( ... ), so that its address can
be passed forth and back across my code?

Regards,
Giovanni
// ---------------------------------------------- the file
function.cpp
#include <boost/bind.hpp>

struct oneVar_function // one variable function
{
double (* function) (double x, void* params);
void* params;
};

struct twoVar_function // two variables function
{
double (* function) (double x, double y, void* params);
void* params;
};

double
my_function (double x, double y, void* params)
{
// I cast `params` to double*.
// The user will be kind enough to always
// give a double* as 3rd argument when
// calling my_function.
double* alpha = static_cast<dou ble*>(params);
return x + y + *alpha; // or whatever
}

int main()
{
twoVar_function f;
oneVar_function g;

f.function = &my_function ;
double alpha = 3;
f.params = &alpha;

double a_given_number = 2;
g.function = &(boost::bind(* (f.function),
_1,
a_given_number,
_2));
g.params = &alpha;
}
// -------------------------------------- end of the file function.cpp
Aug 28 '08 #1
3 3335
On Aug 28, 2:18*pm, Giovanni Gherdovich
<gherdov...@stu dents.math.unif i.itwrote:
Hello,

in the following code I have a pointer (to function), say p,
of type
double (*)(double, double, void*)
and I try to fix the second argument of the function *p
to a given value (using boost::bind), but the compiler
complains, because of a type mismatch in an assignment
which I think should be legal:

/*
** *bash-3.00$ g++ function.cpp
** *function.cpp: In function `int main()':
** *function.cpp:3 6: warning: taking address of temporary
** *function.cpp:3 6: error: cannot convert
** * * * * `boost::_bi::bi nd_t<double,
** * * * * * * * * * * * * * * double (*)(double, double, void*),
** * * * * * * * * * * * * * * boost::_bi::lis t3<boost::arg<1 >,
**
boost::_bi::val ue<double>,
** * * * * * * * * * * * * * * * * * * * * * * * boost::arg<2 >*'
** * * * * * to `double (*)(double, void*)' in assignment
**/
The compiler message says that the result of boost::bind is not
convertible to a function pointer.
boost::bind is a function template which returns an object (the type
of this object is a function of the parameters passed to the
boost::bind invocation), not an "ordinary" function.

The warning about taking the address of a temporary is because your
code takes the address of that object returned by boost::bind.
How can I build a "real" (I mean non-temporary) object
out of boost::bind( ... ), so that its address can
be passed forth and back across my code?
Have a look at boost::function : http://www.boost.org/doc/libs/1_36_0.../function.html

HTH,

Éric Malenfant
Aug 28 '08 #2
On Aug 28, 8:18*pm, Giovanni Gherdovich
<gherdov...@stu dents.math.unif i.itwrote:
Hello,

in the following code I have a pointer (to function), say p,
of type
double (*)(double, double, void*)
and I try to fix the second argument of the function *p
to a given value (using boost::bind), but the compiler
complains, because of a type mismatch in an assignment
which I think should be legal:
<snip>
>
Can you see why? The code is below.

There is also a warning which I don't like at all,
and remebers me that boost::bind( ... ) is a temporary
object and its address makes sense only in the current
scope (if I'm not wrong).
Since I have to pass this address as argument to another
function (later, in my real code), this can couse problems.

How can I build a "real" (I mean non-temporary) object
out of boost::bind( ... ), so that its address can
be passed forth and back across my code?
<snip>
struct oneVar_function *// one variable function
{
* double (* function) (double x, void* params);
* void* params;

};
<snip>
>
* double a_given_number = 2;
* g.function = &(boost::bind(* (f.function),
* * * * * * * * * * * * * * *_1,
* * * * * * * * * * * * * * *a_given_number ,
* * * * * * * * * * * * * * *_2));
'Bind' does not return a function pointer but an unspecified function
object (sort of a closure).

Thus there are two problems: first the assignment is illegal: you are
trying to convert a pointer to such a function object to a pointer to
a function. Second, as the compiler warns you, grabbing the address of
the temporary object returned by bind is a dangerous action, because
the temporary will be destroyed at the end of the expression.

As the exact result type of 'bind' is unspecified (well, you can see
it in the error message, but you shouldn't rely on it), you will have
to use boost::function as the type of the 'function' member of
oneVar_function :

#include <boost/function.hpp>
struct oneVar_function // one variable function
{
boost::function <double (double x, void* params)function ;
void* params;
};

[note: unteste code]
Note that boost::function can store function pointers as well as
function objects.

--
gpd

Aug 28 '08 #3
Hello,

thank you for your answers.

Eric:
Have a look at boost::function
gpd:
As the exact result type of 'bind' is unspecified (well, you can see
it in the error message, but you shouldn't rely on it), you will have
to use boost::function as the type of the 'function' member of
oneVar_function : [...]
I implemented my (wanna-be) callbacks as boost::function instead of
function pointers, and the two problems I mentioned in my previous
post are gone.

Furthermore I feel that switching the whole design of my real code
to boost::function will be straightforward . Cool!

Here is the modified version of my toy example, which compiles
and works fine:

// ---------------- this is the file function2.cpp
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>

struct oneVar_function // one variable function
{
boost::function <double (double x, void* params)function ;
void* params;
};

struct twoVar_function // two variables function
{
boost::function <double (double x, double y, void* params)function ;
void* params;
};

double
my_function (double x, double y, void* params)
{
// I cast `params` to double*.
// The user will be kind enough to always
// give a double* as 3rd argument when
// calling my_function.
double* alpha = static_cast<dou ble*>(params);
return x + y + *alpha; // or whatever
}

int main()
{
double alpha = 3;
twoVar_function f;
oneVar_function g;

f.function = &my_function ;
f.params = &alpha;

double a_given_number = 2;
g.function = boost::bind(f.f unction,
_1,
a_given_number,
_2);
g.params = &alpha;

std::cout << (f.function)(1, 2, f.params) << std::endl;
// expected: 6
std::cout << (g.function)(1, g.params) << std::endl;
// expected: 6

/*
* bash-3.00$ g++ function2.cpp
* bash-3.00$ ./a.out
* 6
* 6
* bash-3.00$
*/
}
// ---------------- end of file function2.cpp
Aug 28 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2729
by: Arturo Cuebas | last post by:
The program below contains a compile error. Following the program you will find the typical fix then my idea for a library that facilitates a more elegant fix. #include <boost\bind.hpp> using namespace boost; struct C {
4
1809
by: rapataa | last post by:
hi, I'm trying to fill a collection using the following 'generic' code: ----------------- public class baseCollection : System.Collections.CollectionBase { protected void Fill(string strSQL, object oTest) { SqlConnection Conn = new SqlConnection(Settings.sDBConn); Conn.Open();
0
1030
by: Russell Hind | last post by:
Is it possible to create a boost::bind object for a managed class method? With non-managed classes, I can do boost::function<void ()> f = boost::bind(&Class_c::Function, this); Is there a way to do this for __gc classes? I do want to be able to assign the result to a boost::function, but can write my own wrapper if necessary. Thanks
1
1450
by: andrewcw | last post by:
Where is the font size and type set for DataGrid in WINFORM. Thanks ( DataGridTableStyle ? or the DataGrid ? Its in the property page - but can it be set programmatically ? How ? Thanks
1
2314
by: ciruliz ciruliz | last post by:
In Excel there is one greate feature - I can create table with ole object field type & put excel document in it, then in table view when double-clicking on that field , excel document opens in excel window how can i do this in VB.NET ? I've found in Ms Access macros , I can do this with ole container object, launching external excel from it automatically. but there is no ole support in VB.NET. Any ideas?
1
4418
by: Thomas D. | last post by:
Hello all, I'm using the IXmlSerializable interface for a project and encounter some problems when testing my webservice in a client application. I know this interface is undocumented and not intended for use, but I think this is the only solution for my situation. I searched the web, in the hope finding the answer, without any luck, so my final hope is with you. Let me explain the situation: I have an 'export'-wrapper to my regular...
2
2264
by: IndyStef | last post by:
I am trying to use boost's bind on a member function, on the VC8 compiler. After using several different attempts, I could not get it to work. Does anybody know what is wrong with the code below? The function that doesn't compile is foo::DoTheStuff. All three variations of the for-each loop won't build.
1
2149
by: Sky | last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but be Type, AssemblyName. Fair enough, get the reason. (Finally!). As long as it doesn't cause tech support problems down the line... What happens when my code is run on a station that only has framework 3.0 or 4.0, and this assembly, with version number defined for 2.0.0.0 , isn't available. ...breaks? Second question: Does an assembly's PublicKeyToken change...
0
1331
by: XHengDF | last post by:
I am a new gay to use the library boost, so i confused by some details! now, anybody could help me to explain the difference between boost::bind and boost::lambda::bind when i use the library. I mean when one will work but another don't. they all seem to be a functor,right? am i send to the rigjt group?
0
8652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8983
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6575
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4412
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2036
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.