Trouble with template on boost::function and boost::bind
I tried to create a template function that receives boost::function as parameter. Say like this: template <typename T1, typename T2> void proc(const boost::function<void(const T1&, T2&)> &p) { // ... } void f(const string& s1, string&s2) { // ... } Everything goes ok when I called proc( boost::function<void(const string&, string&)>(f) ). But when I try this: proc(boost::bind(f, _1, _2)); The compiler refused with the error: no matching function for call to ‘proc(boost::_bi::bind_t<boost::_bi::unspecified, F, boost::_bi::list2<boost::arg<1>, boost::arg<2> > >)’ Anyway to fulfill this design purpose? Thanks.
On Sat, Mar 10, 2012 at 11:56 PM, pwx <airyai@gmail.com> wrote:
I tried to create a template function that receives boost::function as parameter. Say like this:
template <typename T1, typename T2> void proc(const boost::function<void(const T1&, T2&)> &p) { // ... }
void f(const string& s1, string&s2) { // ... }
Everything goes ok when I called proc( boost::function<void(const string&, string&)>(f) ). But when I try this:
proc(boost::bind(f, _1, _2));
The compiler refused with the error:
no matching function for call to ‘proc(boost::_bi::bind_t<boost::_bi::unspecified, F, boost::_bi::list2<boost::arg<1>, boost::arg<2> > >)’
Anyway to fulfill this design purpose? Thanks.
Explicitly provide the template parameters? E.g., proc< std::string, std::string >(boost::bind(f, _1, _2)); There's really no way for the compiler to figure out what T1 and T2 should be without your help. Or maybe your proc should really be declared like template< class F > void proc(F const & f); ? Or maybe you can provide the above overload of proc and have its body provide the explicit template parameters to the original overload of proc for you? Just some ideas. - Jeff
Ah, yes, it really works if I explicitly provide the template parameters. What a pity that the extremely beautiful code like f(boost::bind) is not supported! Anyway, it's ok to write something like p<string, string>(boost::bind(f, _1, _2)), which is far better than p(boost::function<void(string,string)>(boost::bind(f, _1, _2)). Thanks for your help! 2012/3/11 Jeffrey Lee Hellrung, Jr. <jeffrey.hellrung@gmail.com>
On Sat, Mar 10, 2012 at 11:56 PM, pwx <airyai@gmail.com> wrote:
I tried to create a template function that receives boost::function as parameter. Say like this:
template <typename T1, typename T2> void proc(const boost::function<void(const T1&, T2&)> &p) { // ... }
void f(const string& s1, string&s2) { // ... }
Everything goes ok when I called proc( boost::function<void(const string&, string&)>(f) ). But when I try this:
proc(boost::bind(f, _1, _2));
The compiler refused with the error:
no matching function for call to ‘proc(boost::_bi::bind_t<boost::_bi::unspecified, F, boost::_bi::list2<boost::arg<1>, boost::arg<2> > >)’
Anyway to fulfill this design purpose? Thanks.
Explicitly provide the template parameters? E.g.,
proc< std::string, std::string >(boost::bind(f, _1, _2));
There's really no way for the compiler to figure out what T1 and T2 should be without your help. Or maybe your proc should really be declared like
template< class F > void proc(F const & f);
?
Or maybe you can provide the above overload of proc and have its body provide the explicit template parameters to the original overload of proc for you?
Just some ideas.
- Jeff
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- 平芜尽处是春山,行人更在春山外。泫,落水成泪。
participants (3)
-
Jeffrey Lee Hellrung, Jr.
-
pwx
-
平芜泫