views:

169

answers:

3

I have a Visual Studio 2008 C++ application that does something like this:

template< typename Fcn >
inline void Bar( Fcn fcn )  // line 84
{
    fcn();
};

template< typename Fcn >
inline void Foo( Fcn fcn )
{
    // this works fine
    Bar( fcn );

    // this fails to compile
    boost::bind( Bar, fcn )();
};

int main()
{
    SYSTEM_POWER_STATUS_EX status = { 0 };
    Foo( boost::bind( ::GetSystemPowerStatusEx, &status, true ) );  // line 160
    return 0;
}

*The call to GetSystemPowerStatusEx() is just for demonstration. Insert your favorite call there and the behavior is the same.

When I go to compile this, I get 84 errors. I won't post them all unless asked, but they start with this:

1>.\MyApp.cpp(99) : error C2896: 'boost::_bi::bind_t<_bi::dm_result<MT::* ,A1>::type,boost::_mfi::dm<M,T>,_bi::list_av_1<A1>::type> boost::bind(M T::* ,A1)' : cannot use function template 'void Bar(Fcn)' as a function argument
1>        .\MyApp.cpp(84) : see declaration of 'Bar'
1>        .\MyApp.cpp(160) : see reference to function template instantiation 'void Foo<boost::_bi::bind_t<R,F,L>>(Fcn)' being compiled
1>        with
1>        [
1>            R=BOOL,
1>            F=BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),
1>            L=boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>,
1>            Fcn=boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>
1>        ]

If anybody can point out what I may be doing wrong, I would appreciate it.


Edit: By changing to:

boost::bind( Bar< Fcn >, fcn );

as suggested by everybody, I'm down to 1 error:

1>boost/bind/bind.hpp(246) : error C2664: 'void (Fcn)' : cannot convert parameter 1 from 'int' to 'boost::_bi::bind_t<R,F,L>'
1>        with
1>        [
1>            Fcn=boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>
1>        ]
1>        and
1>        [
1>            R=BOOL,
1>            F=BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),
1>            L=boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>
1>        ]
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>        boost/bind/bind_template.hpp(20) : see reference to function template instantiation 'void boost::_bi::list1<A1>::operator ()<void(__cdecl *)(Fcn),boost::_bi::list0>(boost::_bi::type<T>,F &,A &,int)' being compiled
1>        with
1>        [
1>            A1=boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>,
1>            Fcn=boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>,
1>            T=void,
1>            F=void (__cdecl *)(boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>),
1>            A=boost::_bi::list0
1>        ]
1>        boost/bind/bind_template.hpp(18) : while compiling class template member function 'void boost::_bi::bind_t<R,F,L>::operator ()(void)'
1>        with
1>        [
1>            R=void,
1>            F=void (__cdecl *)(boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>),
1>            L=boost::_bi::list1<boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>>
1>        ]
1>        .\MyApp.cpp(99) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1>        with
1>        [
1>            R=void,
1>            F=void (__cdecl *)(boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>),
1>            L=boost::_bi::list1<boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>>
1>        ]
1>        .\MyApp.cpp(160) : see reference to function template instantiation 'void Foo<boost::_bi::bind_t<R,F,L>>(Fcn)' being compiled
1>        with
1>        [
1>            R=BOOL,
1>            F=BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),
1>            L=boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>,
1>            Fcn=boost::_bi::bind_t<BOOL,BOOL (__cdecl *)(PSYSTEM_POWER_STATUS_EX,BOOL),boost::_bi::list2<boost::_bi::value<_SYSTEM_POWER_STATUS_EX *>,boost::_bi::value<bool>>>
1>        ]

Thanks, PaulH

+3  A: 

You need to specify the template parameter.

boost::bind( Bar<Fcn>, fcn )();

Edit: Further, one has to use boost::protect to prevent fcn from getting evaluated. See Paul's comment for the correct usage.

kloffy
Thanks! Just one error to go though. cannot convert parameter 1 from 'int' to 'boost::_bi::bind_t<R,F,L>
PaulH
@kloffy - you are very close. I'd never heard of boost::protect before, but I loooked it up. What I needed was `Foo( boost::protect( boost::bind( ::GetSystemPowerStatusEx, `
PaulH
Yeah, it's not very well documented. At first, I tried getting around the evaluation using `boost::bind(Bar<Fcn>, boost::ref(fcn))();` and it worked, too. I quickly googled whether there is a solution in boost::bind and found a post on the mailing list (http://lists.boost.org/boost-users/2004/11/8412.php). But, as you already figured out, it wasn't quite the right answer either.
kloffy
+3  A: 

The problem is that Bar is not a function. Bar<Fcn> however is. Try

boost::bind( Bar<Fcn>, fcn )();
avakar
+1  A: 

try boost::bind( Bar< Fcn >, fcn )();

bb