function object wrapper problem

I have a class that has a constructor that takes a Boost function object wrapper; class some_class { public: some_class(const boost::function1<wchar_t*, short*>& some_member) .... I call the constructor with a member function using Boost bind. some_class some_object(boost::bind(&some_other_class::some_member, &some_other_object, _1)); Now I want to use some_class for some_member functions that return an other type pointer. So I do class some_class { public: template <class T> some_class(const boost::function1<T*, short*>& get_member) .... But now the compiler (VS 2003) starts complaining; error C2664: 'some_class::some_class(const some_class &)' : cannot convert parameter 1 from 'boost::_bi::bind_t<R,F,L>' to 'const some_class &' with [ R=wchar_t *, F=boost::_mfi::mf1<wchar_t *,some_other_class,short *>, L=boost::_bi::list2<boost::_bi::list_av_2<some_other_class *__w64 ,boost::arg<1>>::B1,boost::_bi::list_av_2<some_other_class *__w64 ,boost::arg<1>>::B2> ] Reason: cannot convert from 'boost::_bi::bind_t<R,F,L>' to 'const some_class' with [ R=wchar_t *, F=boost::_mfi::mf1<wchar_t *,some_other_class,short *>, L=boost::_bi::list2<boost::_bi::list_av_2<some_other_class *__w64 ,boost::arg<1>>::B1,boost::_bi::list_av_2<some_other_class *__w64 ,boost::arg<1>>::B2> ] No constructor could take the source type, or constructor overload resolution was ambiguous Any ideas? Many thanks, Okko Willeboordse

Okko Willeboordse wrote:
So I do
class some_class { public: template <class T> some_class(const boost::function1<T*, short*>& get_member)
Why not just template<class F> some_class( F f ); ? boost::function<> is useful when you have to capture an arbitrary function object in a fixed type. Since your boost::function<> above is templated on T, it has a variable type, so you can just use the function object argument as-is.

Thanks, Then I end up with a constructor that takes any type of data which can not be distinguished from my other constructors. Peter Dimov wrote:
Okko Willeboordse wrote:
So I do
class some_class { public: template <class T> some_class(const boost::function1<T*, short*>& get_member)
Why not just
template<class F> some_class( F f );
?
boost::function<> is useful when you have to capture an arbitrary function object in a fixed type. Since your boost::function<> above is templated on T, it has a variable type, so you can just use the function object argument as-is.

On Aug 29, 2005, at 4:44 PM, Okko Willeboordse wrote:
Peter Dimov wrote:
Okko Willeboordse wrote:
So I do
class some_class { public: template <class T> some_class(const boost::function1<T*, short*>& get_member)
Why not just
template<class F> some_class( F f );
?
boost::function<> is useful when you have to capture an arbitrary function object in a fixed type. Since your boost::function<> above is templated on T, it has a variable type, so you can just use the function object argument as-is.
Thanks,
Then I end up with a constructor that takes any type of data which can not be distinguished from my other constructors.
I'm not sure what your other constructors look like, but perhaps you could constrain F such that there is no question that the templated constructor will accidently bind to the wrong call. For example: class some_class { public: some_class(int i); template <class T> template<class F> some_class( F f, typename enable_if<!is_convertible<F, int>::value>::type* = 0); }; -Howard

Thanks, But also without any other constructor (for both some_class and some_other_class) I get the compiler error. I did another test. I created a function with exactly the same signature as the some_class constructor; template <class T> some_func(const boost::function1<T*, short*>& some_member) { } and now the compiler says; error C2784: 'int some_func(const boost::function1<T*,short*> &)' : could not deduce template argument for 'const boost::function1<R*,short*> &' from 'boost::_bi::bind_t<R,F,L>' with [ R=short *, F=boost::_mfi::mf1<short *,some_other_class,short *>, L=boost::_bi::list2<boost::_bi::list_av_2<some_other_class *__w64 ,boost::arg<1>>::B1,boost::_bi::list_av_2<some_other_class *__w64 ,boost::arg<1>>::B2> ] c:\Temp\com\com.cpp(14) : see declaration of 'some_func' Somehow the compiler does not instantiate the function properly when a templated argument is only fed into the function wrapper. Any ideas? Okko
participants (3)
-
Howard Hinnant
-
Okko Willeboordse
-
Peter Dimov