2

次のようなことを行う Visual Studio 2008 C++ アプリケーションがあります。

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;
}

*GetSystemPowerStatusEx() の呼び出しはデモ用です。そこにお気に入りの呼び出しを挿入すると、動作は同じです。

これをコンパイルすると、84 エラーが発生します。尋ねられない限り、すべてを掲載することはしませんが、次のように始まります。

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>        ]

私が間違っているかもしれないことを誰かが指摘できれば、私はそれを感謝します。


編集:次のように変更することにより:

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

みんなが示唆しているように、私は1つのエラーにまでなっています:

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>        ]

ありがとう、ポールH

4

3 に答える 3

5

テンプレート パラメータを指定する必要があります。

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

編集:さらに、boost::protect を使用して fcn が評価されないようにする必要があります。正しい使用法については、ポールのコメントを参照してください。

于 2010-04-12T20:48:37.807 に答える
3

問題は、それBarが関数ではないことです。Bar<Fcn>しかしそうです。試す

boost::bind( Bar<Fcn>, fcn )();
于 2010-04-12T20:49:13.467 に答える
1

boost::bind( Bar< Fcn >, fcn )(); を試してください。

于 2010-04-12T20:51:47.627 に答える