std::bind - Should this code work?

Started by
10 comments, last by Codarki 11 years, 7 months ago

Why don't you report it to Microsoft Connect?



And if you do report it, would you mind posting the link? I wouldn't mind tracking it to see what they say.


Sure thing. http://connect.microsoft.com/VisualStudio/feedback/details/763571/msvs-2012-c-std-bind-illegal-indirection-compiler-error


If this is your actual code, you should call std::mem_fn and not std::bind since you aren't binding anything, mem_fn will give you a callable object which takes either a pointer or reference to the object as it's first parameter. That said, I can't find any reason why it shouldn't work. If you explicitly handle the conversion yourself using mem_fn, it also works


You're right - that was one thing that I tried early on and it does work properly. It's probably the appropriate solution if we want to be as correct as possible. It also appears that the issue goes away if we do in fact use bind appropriately (we actually bind something) like so:


virtual void ObjectBase::vfunc_parm(int i) const { }
void ObjectDerived::vfunc_parm(int i) const { std::cout << "vfunc_parm i = " << i << std::endl;}

const ObjectDerived obj;
auto func = std::bind(&ObjectBase::vfunc_parm, _1, _2);
func(obj, 2);


The only downside is that this is a problem in many thousands of locations and determining in each location the appropriate solution will be a tedious process; even if it does make our code base better overall.

It's as much a matter of curiosity for me at this point anyways. Conceptually I don't understand why the original code wouldn't work, even if it's not technically the best solution.
Advertisement

I believe your quote is referring to the arguments actually passed to the std::bind call.


You're right my quote from documentation said arguments for bind is copied.

But I was merely pointing to the compiler output, that it's taking the ObjectDerived as value:

c:\users\ryan\documents\visual studio 2012\projects\cppeleventest\cppeleventest\std_bind_test.cpp(19) : see reference to function template instantiation 'void std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()<const ObjectDerived&>(const ObjectDerived)' being compiled
[/quote]

I checked the implementation of that for VS2010, and it seemed like it is taking the parameter as rvalue reference, and doing perfect forwarding to some Apply function. I don't have source code for VS2012 atm, so there might be a bug. Bug at std::bind is major, so I think I'm mistaken. And I'm not entirely sure about the purpose of the template specialization for "const ObjectDerived&" there, but no trickery can access the copied from object.

I meant to reply this with more detailed answer, but it was a busy day. And possibly I'm analyzing this wrong.

Btw, why the hell isn't the MS bug database public anymore?

This topic is closed to new replies.

Advertisement