升压signals2自动连接管理和改变信号的互斥体类型 [英] Boost signals2 automatic connection management and changing the mutex type of a signal
问题描述
我试图用<一个href=\"http://www.boost.org/doc/libs/1_40_0/doc/html/signals2/tutorial.html#signals2.tutorial.connection-management\"相对=nofollow>自动连接管理和<一个href=\"http://www.boost.org/doc/libs/1_40_0/doc/html/signals2/tutorial.html#signals2.tutorial.signal-mutex-template-parameter\"相对=nofollow>改变信号的互斥体类型模板函数。
以下code编译和使用gcc-4.3.4执行罚款。 (http://ideone.com/LLN6d)
的#include&LT;升压/ signals2.hpp&GT;
#包括LT&;升压/ enable_shared_from_this.hpp&GT;
#包括LT&;&stdio.h中GT;类简单:公众的boost :: enable_shared_from_this&LT;简单&GT;
{
私人的:
TYPEDEF提振:: signals2 :: signal_type&LT;无效(INT)
提高:: signals2 ::关键词:mutex_type&LT;提高:: signals2 :: dummy_mutex&GT; &GT; ::类型sig_type; sig_type SIG;上市:
模板&LT;类型名楼typename的T,typename的A1&GT;
无效代理(F F,T T,A1 A1)
{
提振::函数&LT;无效()&GT;仿=的boost ::绑定(F,T,A1); //这得到其他地方传递消息
仿函数();
} 无效FUNC(int i)以
{
++我; 的printf(%D,我);
} 模板&LT;类型名楼typename的T,typename的A1&GT;
提高:: signals2 ::连接来连接(F F,T T,A1 A1)
{
回报sig.connect
(
提高::绑定
(
&安培;简单::代理&LT; F,T,INT&gt;中
T,
F,
T,
A1
)
);
} 模板&LT;类型名楼typename的ST,typename的A1&GT;
提高:: signals2 ::连接connectAutoMgmt(F楼ST圣方,A1 A1)
{
回报sig.connect
(
sig_type :: slot_type
(
&安培;简单::代理&LT;男,ST,INT&gt;中
st.get(),
F,
ST,//也许使用了weak_ptr(?)
A1
).track(ST)
);
} 无效的init()
{
提高:: shared_ptr的&LT;简单&GT; S0(新的Simple);
提高:: signals2 ::连接C0 = sig.connect
(
sig_type :: slot_type(安培;简单:: FUNC,s0.get(),_1).track(S0)
);
签名(11);
c0.disconnect(); 提高:: shared_ptr的&LT;简单&GT; S1(新的Simple);
提高:: signals2 ::连接c1 =连接(安培;简单:: FUNC,s1.get(),_1);
SIG(22);
c1.disconnect(); 提高:: shared_ptr的&LT;简单&GT; S2(新的Simple);
提高:: signals2 ::连接C2 = sig.connect
(
sig_type :: slot_type
(
&安培;简单::代理&LT;无效(简单:: *)(INT),提高:: shared_ptr的&LT;简单&gt;中INT&gt;中
s2.get(),
&安培;简单:: FUNC,
S2,
_1
).track(S2)
); //错误
签名(33);
c2.disconnect(); 提高:: shared_ptr的&LT;简单&GT; S3(新的Simple);
提高:: signals2 ::连接C3 = connectAutoMgmt(安培;简单:: FUNC,S3,_1); //错误
签名(44);
c3.disconnect(); }
};诠释的main()
{
。简单的()的init();
}
但是,Visual Studio 2008中下,我得到以下编译器错误。有什么建议么?谢谢你。
错误C2665:提振:: signals2 :: SLOT1&LT; R,T1,SlotFunction&GT; :: SLOT1':没有2重载可以转换所有的参数类型
同
[
R =无效,
T1 = INT,
SlotFunction =的boost ::功能&LT;无效(INT)&GT;
]
提升/ signals2 /细节/ slot_template.hpp(98):可以是提振:: signals2 :: SLOT1&LT; R,T1,SlotFunction&GT; :: SLOT1&LT;无效(F,T,A1),简单*,无效(__ thiscall简单:: *)(INT),提高:: shared_ptr的&LT;简单&GT;,提振:: ARG&LT; I&GT;&GT;(Func键(安培),常量BindArgT1和放大器;,常量BindArgT2和放大器;,常量BindArgT3和放大器;,常量BindArgT4和放大器;)
同
[
R =无效,
T1 = INT,
SlotFunction =的boost ::功能&LT;无效(INT)&gt;中
F =无效(__thiscall简单:: *)(INT)
T =的boost :: shared_ptr的&LT;简单&gt;中
A1 = INT,
I = 1,
FUNC =无效(无效(__thiscall简单:: *)(INT),提高:: shared_ptr的&LT;简单&GT;,INT)
BindArgT1 =简单*
BindArgT2 =无效(__thiscall简单:: *)(INT)
BindArgT3 =升压:: shared_ptr的&LT;简单&gt;中
BindArgT4 =的boost :: ARG&LT; 1 GT;
]
而试图匹配参数列表(重载功能,简单*,无效(__thiscall简单:: *)(INT),提高:: shared_ptr的&LT;吨&gt;中的boost :: ARG&LT; I&GT;)
同
[
T =简单
]
和
[
I = 1
]
错误C2228:左'.track'必须有类/结构/联合
我检查了code,但找不到错误的直接原因。
通过一次分配&放大器;简单::代理
来等价变量如下,
VC编译code莫名其妙:
模板&LT;类型名楼typename的ST,typename的A1&GT;
提高:: signals2 ::连接connectAutoMgmt(F楼ST圣方,A1 A1){
无效(简单:: * P)(F,ST,INT)=&安培;简单::代理;
返回sig.connect(sig_type :: slot_type(P,....).track(ST));
}无效的init(){
....
无效(简单:: * P)(
无效(简单:: *)(INT),提高:: shared_ptr的&LT;简单&gt;中INT)=
&安培;简单::代理; 提高:: signals2 ::连接C2 = sig.connect(
sig_type :: slot_type(P,...).track(S2));
....
不过,我想不通为什么上面的变化都有影响。
老实说,我怀疑VC的错误的时刻...无论如何,希望这有助于。
I am trying to use automatic connection management and changing the mutex type of a signal for a template function.
The following code compiles and executes fine using gcc-4.3.4. (http://ideone.com/LLN6d)
#include <boost/signals2.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <stdio.h>
class Simple : public boost::enable_shared_from_this< Simple >
{
private:
typedef boost::signals2::signal_type<void( int ),
boost::signals2::keywords::mutex_type< boost::signals2::dummy_mutex > >::type sig_type;
sig_type sig;
public:
template < typename F, typename T, typename A1 >
void proxy( F f, T t, A1 a1 )
{
boost::function< void() > functor = boost::bind( f, t, a1 ); // this gets messaged elsewhere
functor();
}
void func( int i )
{
++i;
printf( "%d", i );
}
template < typename F, typename T, typename A1 >
boost::signals2::connection connect( F f, T t, A1 a1 )
{
return sig.connect
(
boost::bind
(
&Simple::proxy< F, T, int >,
t,
f,
t,
a1
)
);
}
template < typename F, typename ST, typename A1 >
boost::signals2::connection connectAutoMgmt( F f, ST st, A1 a1 )
{
return sig.connect
(
sig_type::slot_type
(
&Simple::proxy< F, ST, int >,
st.get(),
f,
st, // maybe use weak_ptr (?)
a1
).track( st )
);
}
void init()
{
boost::shared_ptr< Simple > s0( new Simple );
boost::signals2::connection c0 = sig.connect
(
sig_type::slot_type( &Simple::func, s0.get(), _1 ).track( s0 )
);
sig( 11 );
c0.disconnect();
boost::shared_ptr< Simple > s1( new Simple );
boost::signals2::connection c1 = connect( &Simple::func, s1.get(), _1 );
sig( 22 );
c1.disconnect();
boost::shared_ptr< Simple > s2( new Simple );
boost::signals2::connection c2 = sig.connect
(
sig_type::slot_type
(
&Simple::proxy< void (Simple::*)(int), boost::shared_ptr< Simple >, int >,
s2.get(),
&Simple::func,
s2,
_1
).track( s2 )
); // error
sig( 33 );
c2.disconnect();
boost::shared_ptr< Simple > s3( new Simple );
boost::signals2::connection c3 = connectAutoMgmt( &Simple::func, s3, _1 ); // error
sig( 44 );
c3.disconnect();
}
};
int main()
{
Simple().init();
}
However, under Visual Studio 2008, I get the following compiler errors. Any suggestions? Thanks.
error C2665: 'boost::signals2::slot1<R,T1,SlotFunction>::slot1' : none of the 2 overloads could convert all the argument types
with
[
R=void,
T1=int,
SlotFunction=boost::function<void (int)>
]
boost/signals2/detail/slot_template.hpp(98): could be 'boost::signals2::slot1<R,T1,SlotFunction>::slot1<void(F,T,A1),Simple*,void(__thiscall Simple::* )(int),boost::shared_ptr<Simple>,boost::arg<I>>(Func (&),const BindArgT1 &,const BindArgT2 &,const BindArgT3 &,const BindArgT4 &)'
with
[
R=void,
T1=int,
SlotFunction=boost::function<void (int)>,
F=void (__thiscall Simple::* )(int),
T=boost::shared_ptr<Simple>,
A1=int,
I=1,
Func=void (void (__thiscall Simple::* )(int),boost::shared_ptr<Simple>,int),
BindArgT1=Simple *,
BindArgT2=void (__thiscall Simple::* )(int),
BindArgT3=boost::shared_ptr<Simple>,
BindArgT4=boost::arg<1>
]
while trying to match the argument list '(overloaded-function, Simple *, void (__thiscall Simple::* )(int), boost::shared_ptr<T>, boost::arg<I>)'
with
[
T=Simple
]
and
[
I=1
]
error C2228: left of '.track' must have class/struct/union
I examined the code, but couldn't find the immediate cause of the error.
By once assigning &Simple::proxy
to equivalent variable as the following,
VC compiled the code somehow:
template < typename F, typename ST, typename A1 >
boost::signals2::connection connectAutoMgmt( F f, ST st, A1 a1 ) {
void (Simple::*p)( F, ST, int ) = &Simple::proxy;
return sig.connect( sig_type::slot_type( p, .... ).track( st ) );
}
void init() {
....
void (Simple::*p)(
void(Simple::*)(int), boost::shared_ptr< Simple >, int ) =
&Simple::proxy;
boost::signals2::connection c2 = sig.connect(
sig_type::slot_type( p, .... ).track( s2 ) );
....
However, I cannot figure out why the above changes have an effect. Honestly I suspect VC's bug at the moment... Anyway, hope this helps.
这篇关于升压signals2自动连接管理和改变信号的互斥体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!