|
Boost Users : |
From: jbd (evadream.mlist_at_[hidden])
Date: 2006-05-12 08:05:56
Hello everybody,
First of all, i'm a beginner trying to understand the use of the
boost::bind library. Here is my problem :
I'd like to know if there is a way to omit placeholders when calling
boost::bind and let the compiler deduce their number. I understand that
this is contrary to the flexibility offers by the placeholder, but i need
to do something like that.
void bar() {}
void bar2(int) {}
// Compile and works well
boost::function < void ()> f1( boost::bind(&bar) ) ;
boost::function < void (int) > f2( boost::bind(&bar2,_1) ) ;
// i'd like to write this, but it doesn't compile
// without the placeholders
boost::function < void (int) > f2( boost::bind(&bar2) ) ;
// i can write a small helper function mybind to achieve my goal
// this compile and works well
mybind(bar2) ;
with
template <typename Ret, typename Arg1, typename Arg2>
void mybind(Ret (*Func)(Arg1, Arg2))
{
boost::function<Ret (Arg1, Arg2)> f(Func) ;
}
I hope you understand what i've try to show. Maybe i missed something in
the documentation.
Thank you for reading !
Here is the working .cpp file i use to test
things.
===============================
#include <iostream>
#include <stdexcept>
#include <boost/bind.hpp>
#include <boost/function.hpp>
template <typename Ret, typename Arg1, typename Arg2>
void mybind(Ret (*Func)(Arg1))
{
boost::function<Ret (Arg1)> f(Func) ;
}
void bar()
{
std::cout << "bar()\n" ;
}
void bar2(int a)
{
std::cout << "bar2(int)\n" ;
}
int main (int argc, char* argv[])
{
try
{
// Compile and works well
{
boost::function < void ()> f1( boost::bind(&bar) ) ;
boost::function < void (int) > f2( boost::bind(&bar2,_1) ) ;
}
/*
// i'd like to write this, but it doesn't compile
{
// without the placeholders
boost::function < void (int) > f2( boost::bind(&bar2) ) ;
f2(5) ;
}
*/
// i can write a small helper function mybind to achieve my goal
// this compile and works well
/*
template <typename Ret, typename Arg1, typename Arg2>
void mybind(Ret (*Func)(Arg1, Arg2))
{
boost::function<Ret (Arg1, Arg2)> f(Func) ;
}
*/
{
mybind(bar2) ;
}
}
catch(std::exception& e)
{
std::cout << e.what() << '\n' ;
return -1 ;
}
catch(...)
{
std::cout << "Non standard exception\n" ;
return -1 ;
}
}
========================================
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net