在绑定信号槽的模板函数时使用占位符
Using placeholder when binding a template function for signal slot
我试图在信令期间绑定模板函数。然而,当我在slot函数中使用占位符时,我得到一个"负下标"编译器错误。我正在使用Visual Studio2008. 谢谢。
代码:class My
{
private:
boost::signals2::signal<void (int)> sig;
public:
template < typename F, typename T, typename A1 >
void proxy( F f, T t, A1 a1 )
{
boost::bind( f, t, a1 ); // this gets messaged elsewhere
}
int foo( int i )
{
return i-1;
}
int bar( int i )
{
return i+1;
}
template < typename F, typename T, typename A1 >
boost::signals2::connection connect( F f, T t, A1 a1 )
{
return sig.connect( boost::bind( &My::proxy< F, T, A1 >, t, f, t, a1 ) );
}
void main()
{
boost::signals2::connection c1 = connect( &My::foo, this, 11 ); // ok
boost::signals2::connection c2 = connect( &My::bar, this, _1 ); // negative subscript
}
};
错误:boost/bind/arg.hpp(37) : error C2118: negative subscript
boost/bind/bind.hpp(450) : see reference to function template instantiation 'boost::arg<I>::arg<A1>(const T &)' being compiled
with
[
I=1,
A1=int,
T=int
]
boost/bind/bind_template.hpp(32) : see reference to function template instantiation 'void boost::_bi::list4<A1,A2,A3,A4>::operator ()<F,boost::_bi::list1<int &>>(boost::_bi::type<T>,F &,A &,int)' being compiled
with
[
A1=boost::_bi::value<My *>,
A2=boost::_bi::value<int (__thiscall My::* )(int)>,
A3=boost::_bi::value<My *>,
A4=boost::arg<1>,
F=boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,
T=void,
A=boost::_bi::list1<int &>
]
boost/function/function_template.hpp(153) : see reference to function template instantiation 'void boost::_bi::bind_t<R,F,L>::operator ()<T0>(A1 &)' being compiled
with
[
R=void,
F=boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,
L=boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>,
T0=int,
A1=int
]
boost/function/function_template.hpp(147) : while compiling class template member function 'void boost::detail::function::void_function_obj_invoker1<FunctionObj,R,T0>::invoke(boost::detail::function::function_buffer &,T0)'
with
[
FunctionObj=boost::_bi::bind_t<void,boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>>,
R=void,
T0=int
]
boost/function/function_template.hpp(913) : see reference to class template instantiation 'boost::detail::function::void_function_obj_invoker1<FunctionObj,R,T0>' being compiled
with
[
FunctionObj=boost::_bi::bind_t<void,boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>>,
R=void,
T0=int
]
boost/function/function_template.hpp(722) : see reference to function template instantiation 'void boost::function1<R,T0>::assign_to<Functor>(Functor)' being compiled
with
[
R=void,
T0=int,
Functor=boost::_bi::bind_t<void,boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>>
]
boost/function/function_template.hpp(1065) : see reference to function template instantiation 'boost::function1<R,T0>::function1<F>(Functor,int)' being compiled
with
[
R=void,
T0=int,
F=boost::_bi::bind_t<void,boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>>,
Functor=boost::_bi::bind_t<void,boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>>
]
boost/function/function_template.hpp(1105) : see reference to function template instantiation 'boost::function<Signature>::function<F>(Functor,int)' being compiled
with
[
Signature=void (int),
F=boost::_bi::bind_t<void,boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>>,
Functor=boost::_bi::bind_t<void,boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>>
]
boost/signals2/detail/slot_template.hpp(137) : see reference to function template instantiation 'boost::function<Signature> &boost::function<Signature>::operator =<F>(Functor)' being compiled
with
[
Signature=void (int),
F=boost::_bi::bind_t<void,boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>>,
Functor=boost::_bi::bind_t<void,boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>>
]
boost/signals2/detail/slot_template.hpp(81) : see reference to function template instantiation 'void boost::signals2::slot1<R,T1,SlotFunction>::init_slot_function<F>(const F &)' being compiled
with
[
R=void,
T1=int,
SlotFunction=boost::function<void (int)>,
F=boost::_bi::bind_t<void,boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>>
]
test.cpp(179) : see reference to function template instantiation 'boost::signals2::slot1<R,T1,SlotFunction>::slot1<boost::_bi::bind_t<R,F,L>>(const boost::_bi::bind_t<R,F,L> &)' being compiled
with
[
R=void,
T1=int,
SlotFunction=boost::function<void (int)>,
F=boost::_mfi::mf3<void,My,int (__thiscall My::* )(int),My *,boost::arg<1>>,
L=boost::_bi::list4<boost::_bi::value<My *>,boost::_bi::value<int (__thiscall My::* )(int)>,boost::_bi::value<My *>,boost::arg<1>>
]
test.cpp(196) : see reference to function template instantiation 'boost::signals2::connection My::connect<int(__thiscall My::* )(int),My*,boost::arg<I>>(F,T,A1)' being compiled
with
[
I=1,
F=int (__thiscall My::* )(int),
T=My *,
A1=boost::arg<1>
]
可能是打字错误?问题中的成员函数&My::proxy< F, T, A1 >
取占位符_1
的类型本身,而不是实际参数int
。
在这个例子中,把&My::proxy< F, T, A1 >
变成&My::proxy< F, T, int >
将解决这个错误。
下面是一个关于ideone的测试。
相关文章:
- 将模板与类模板扣除占位符参数匹配
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- 常量和引用成员函数限定符
- Tensorflow C++ 占位符初始化
- std::bind 不会接受 bind 占位符的 std::cref - 为什么?
- qt 中的占位符
- 错误:函数声明符之后的预期函数体
- 我可以在 c++ 中使用占位符作为数组的索引吗?
- 非类型模板参数中的占位符类型是否涉及作为模板参数传递的函数的重载解析?
- 是否可以使用自动占位符来推断非类型模板参数中的函数结果
- 将具有任意参数和占位符的函数存储在类中,然后再调用它
- 在另一个函数中使用std::bind返回对象和占位符作为参数
- 将带有占位符的绑定结果存储在 std::函数中
- std::函数回调,参数采用观察者模式(寄存器主题上的占位符)
- boost::bind() 一个成员函数,用于提升::函数,对象指针作为占位符
- 在对象中存储一个std::函数,该函数包括std::占位符
- 在绑定信号槽的模板函数时使用占位符
- 为类型、函数和变量使用什么占位符名称(foobar)
- c++,将自定义占位符与函数参数匹配
- 为什么在这个例子(成员函数)中没有占位符std::bind不能工作?