2011-10-28 34 views
1

我試圖信號時綁定一個模板函數模板函數時。但是,當我在插槽功能中使用佔位符時,出現「負下標」編譯器錯誤。我使用Visual Studio 2008 感謝。使用綁定的佔位符信號插槽

代碼:

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

回答

0

可能錯字? 在問題中的成員函數&My::proxy< F, T, A1 >取 佔位符_1的類型本身,而不是實際的參數int的。
所以在這種情況下,改變&My::proxy< F, T, A1 >&My::proxy< F, T, int >將解決錯誤。
這裏是ideone測試。

+0

事實上,這是一個「錯字」 ...哈哈。我沒有意識到這是傳遞佔位符的正確方式,儘管現在我明白我必須讓編譯器知道佔位符代表什麼類型。謝謝! – user1005752

+0

不客氣!我自己經常犯這種錯字:) –