bind with mem_fn style in Visual Studio 8
My compiler crashes when I write: bind(&S::bar, &s1); is there some known issue with VS8? It's right after a warning issued in the mem_fn header concerning superfluous modifiers.
On 7/27/2011 3:35 PM, Igor R wrote:
My compiler crashes when I write:
bind(&S::bar,&s1);
is there some known issue with VS8?
Yes, VC8 crashes on incorrect bind expressions. You probably missed some placeholder.
Thanks. Can someone tell me what's wrong? I thought I followed the example from the manual. void foo (int); class S { const char* const name; public: S(const char* x) : name(x) {} void bar (int); }; S s1 ("s1"); // some instance void subscribe (State_t st, const NotifyFunc& func); subscribe (STATE1, &foo); // this works, so the form of the function parameters is right. subscribe (STATE1, bind(&S::bar, &s1)); // this one is wrong. Why?
On Thu, 28 Jul 2011 01:19:11 -0700, John M. Dlugosz
On 7/27/2011 3:35 PM, Igor R wrote:
My compiler crashes when I write:
bind(&S::bar,&s1);
is there some known issue with VS8?
Yes, VC8 crashes on incorrect bind expressions. You probably missed some placeholder.
Thanks. Can someone tell me what's wrong? I thought I followed the example from the manual.
void foo (int);
class S { const char* const name; public: S(const char* x) : name(x) {} void bar (int); };
S s1 ("s1"); // some instance
void subscribe (State_t st, const NotifyFunc& func);
subscribe (STATE1, &foo); // this works, so the form of the function parameters is right.
subscribe (STATE1, bind(&S::bar, &s1)); // this one is wrong. Why?
What's NotifyFunc defined as? What's STATE1 defined as?
On Thu, Jul 28, 2011 at 9:19 AM, John M. Dlugosz
On 7/27/2011 3:35 PM, Igor R wrote:
My compiler crashes when I write:
bind(&S::bar,&s1);
is there some known issue with VS8?
Yes, VC8 crashes on incorrect bind expressions. You probably missed some placeholder.
Thanks. Can someone tell me what's wrong? I thought I followed the example from the manual.
void foo (int);
class S { const char* const name; public: S(const char* x) : name(x) {} void bar (int); };
S s1 ("s1"); // some instance
void subscribe (State_t st, const NotifyFunc& func);
subscribe (STATE1, &foo); // this works, so the form of the function parameters is right.
subscribe (STATE1, bind(&S::bar, &s1)); // this one is wrong. Why?
Does subscribe (STATE1, bind(&S::bar, s1)); work?
Thanks. Can someone tell me what's wrong? I thought I followed the example from the manual.
void foo (int);
class S { const char* const name; public: S(const char* x) : name(x) {} void bar (int); };
S s1 ("s1"); // some instance
void subscribe (State_t st, const NotifyFunc& func);
subscribe (STATE1, &foo); // this works, so the form of the function parameters is right.
subscribe (STATE1, bind(&S::bar, &s1)); // this one is wrong. Why?
Does
subscribe (STATE1, bind(&S::bar, s1));
work?
No, you need to do bind(&S::bar, s1, _1). The _1 in this case means "pass the FIRST argument given to the result of the bind (FIRST because it's _1), as the SECOND argument to &S::bar (SECOND because the _1 appeared as the second argument to bind(), after the function itself). Note that as a member function, bind considers the first argument of &S::bar to be the object on which the member function is called. In other words, if f = bind(&S::bar, s1, _1) then f(x) is equivalent to bar(s1, x) (which is really s1.bar(x)). If you just do f = bind(&S::bar, s1) then f(x) will just be equivalent to bar(s1) (i.e. s1.bar()) (the x will just be ignored), but that is not a valid way to call &S::bar, so it's an error. Regards, Nate.
On 7/28/2011 3:50 AM, Nathan Ridge wrote:
f = bind(&S::bar, s1, _1)
Thanks. I understand what _1 does. I thought that leaving off the real arguments and
giving only the object would produce a function object taking those same arguments. I
guess the compiler can't hand that automatically?
Now I get
V:\boost_1_47\boost/bind/bind.hpp(69) : error C2825: 'F': must be a class or namespace
when followed by '::'
V:\boost_1_47\boost/bind/bind_template.hpp(15) : see reference to class template
instantiation 'boost::_bi::result_traits
V:\boost_1_47\boost/bind/bind.hpp(69) : error C2825: 'F': must be a class or namespace when followed by '::' V:\boost_1_47\boost/bind/bind_template.hpp(15) : see reference to class template instantiation 'boost::_bi::result_traits
' being compiled with [ R=boost::_bi::unspecified, F=void (__thiscall S::* )(uint32_t,call_state_machine::State_t,call_state_machine::State_t) ]
It seems that the number of placeholders still doesn't match the member function signature. What is the exact signature of the *real* member function you bind, and what's the real bind expression? Also, what's the signature of the callable that your subscribe() function expects?
On 7/28/2011 4:58 AM, Igor R wrote:
It seems that the number of placeholders still doesn't match the member function signature.
Ah yes, thank you for helping me see the obvious. While this discussion was going on in email, I added more parameters to the function in my project. I need to update that line, as well! —John
Is your second parameter the address of a class or the class itself? My usual problem with bind is forgetting the 'this' as the second parameter for a member function. Ant -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of John M. Dlugosz Sent: 28 July 2011 12:22 To: boost-users@lists.boost.org Subject: Re: [Boost-users] Still not there yet... On 7/28/2011 4:58 AM, Igor R wrote:
It seems that the number of placeholders still doesn't match the member function signature.
Ah yes, thank you for helping me see the obvious. While this discussion was going on in email, I added more parameters to the function in my project. I need to update that line, as well! —John _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 7/28/2011 6:28 AM, Pace, Antony [Harman Pro Group UK] wrote:
Is your second parameter the address of a class or the class itself? My usual problem with bind is forgetting the 'this' as the second parameter for a member function. Ant
From the documentation, passing the class instance itself will make a copy held inside the function object. Passing the address will store a pointer inside the function object, so it will refer to my instance (which must continue to exist as long as that function object).
From the documentation, passing the class instance itself will make a copy held inside the function object. Passing the address will store a pointer inside the function object, so it will refer to my instance (which must continue to exist as long as that function object).
Oh yes. I didn't realise you could do that. I've learnt something at least! Thanks. Ant
participants (6)
-
Igor R
-
John M. Dlugosz
-
Mostafa
-
Nathan Ridge
-
Pace, Antony [Harman Pro Group UK]
-
Robert Jones