• 12
• 10
• 10
• 13
• 10

# std::bind - Should this code work?

This topic is 2006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In trying to build some existing code from MSVS 2010 in MSVS 2012 I've run into a few (quite a few) compiler errors that were non-existent with the 2010 compiler. Almost all of these errors are in STL/templated code. Trying to figure them out has made me question my sanity, more than anything, but more relevantly my understanding of a few important C++ ideas.

Let me preface this by saying that I understand 2012 is still an RC, but the sheer volume of compiler errors I'm getting is troublesome and makes me wonder if I'm not doing something that if it isn't flat out wrong is at best in a grey area.

One example: A very simple test case to reproduce the compiler error:

 #include "algorithm" #include "functional" class ObjectBase { public: virtual void vfunc() const { } }; class ObjectDerived : public ObjectBase { public: void vfunc() const { } }; int main(int argc, char *argv[]) { using namespace std::placeholders; const ObjectDerived obj; auto func = std::bind(&ObjectBase::vfunc, _1); func(obj); return 0; } 

This code produces
ClCompile:
std_bind_test.cpp
c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1264): error C2100: illegal indirection
c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1147) : see reference to function template instantiation '_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()<ObjectDerived>(const _Wrapper &) const' being compiled
with
[
_Rx=void,
_Pmf_t=void (__thiscall ObjectBase::* )(void) const,
_Farg0=ObjectBase,
_V0_t=std::_Nil,
_V1_t=std::_Nil,
_V2_t=std::_Nil,
_V3_t=std::_Nil,
_V4_t=std::_Nil,
_V5_t=std::_Nil,
<unnamed-symbol>=std::_Nil,
_Wrapper=ObjectDerived
]
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
with
[
_Forced=true,
_Ret=void,
_Fun=std::_Pmf_wrap<void (__thiscall ObjectBase::* )(void) const,void,ObjectBase,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
_V0_t=std::_Ph<1> &,
_V1_t=std::_Nil,
_V2_t=std::_Nil,
_V3_t=std::_Nil,
_V4_t=std::_Nil,
_V5_t=std::_Nil,
<unnamed-symbol>=std::_Nil
]
Build FAILED.

My understanding of it so far is that somewhere down the long line of STL code (in an operator() call I think) the indirection operator is being applied to a non-pointer of type ObjectDerived. I can verify this by implementing the indirection operator for ObjectBase:

 const ObjectBase &operator*() const { return *this; } 

When I do so I can compile and run.

This code compiles and executes fine using the v100 C++ compiler. My question is should this code work? In the 2010 case the code calls vfunc() of the derived class, using obj as the callee, which is what I would expect.

I can work around this simple case pretty easily by calling func(&obj); or func(dynamic_cast<ObjectBase &>(obj)); but I can't, for example, do this:

 std::vector<ObjectDerived> container; container.resize(2); std::for_each(container.begin(), container.end(), std::bind(&ObjectBase::vfunc, _1)); 

I can also workaround by using std::bind(&ObjectDerived::vfunc, _1) but only if that function is actually implemented. Why would binding the derived vs. the base function be resolved differently? (I am actively digging around the STL code to answer that question but that's quite a chore while I have other things to work on as well).

There are a number of potential workarounds but they seem hackish and tedious if the code should be working as written. And again given the sheer volume of template related compiler errors I wonder if I should just be holding off for a later release and hope they get resolved? Edited by Aztral

##### Share on other sites

My question is should this code work?

My gut feeling is no. It is called the 'this pointer' for a reason.

That said, I don't have a reference to back my intuition up. Edited by swiftcoder

##### Share on other sites
Data point: your code compiles and works just peachy on GCC 4.7.

##### Share on other sites
std::function can wrap member functions as (imitating) regular functions; then maybe you can std::bind that?

Code example copy+pasted from Bjarne's webpage:
struct X{ int foo(int); }; function<int (X*, int)> f; f = &X::foo; // pointer to member X x; int v = f(&x, 5); // call X::foo() for x with 5 function<int (int)> ff = std::bind(f,&x,_1); // first argument for f is &x v=ff(5); // call x.foo(5) Edited by Servant of the Lord

##### Share on other sites

My gut feeling is no. It is called the 'this pointer' for a reason.

I'm not sure what you mean. In this case shouldn't obj be the callee and thus it's 'this' pointer used?

Data point: your code compiles and works just peachy on GCC 4.7

I suppose I should have noted this as well - it also compiles just fine with XCode Clang.

std::function can wrap member functions as (imitating) regular functions; then maybe you can std::bind that?

This also compiles in 2010 and fails in 2012. Interestingly, the sample code at http://en.cppreferen...tional/function also fails to compile under MSVS 2012.

It is seeming more and more like an issue with the 2012 compiler, but again I don't know the standard well enough to say whether or not it should compile. It seems like a fundamental and common enough practice that a bug like this would be pretty glaring and wouldn't be present this close to a compiler release date. Edited by Aztral

##### Share on other sites

[quote name='swiftcoder' timestamp='1348082846' post='4981789']
My gut feeling is no. It is called the 'this pointer' for a reason.

I'm not sure what you mean. In this case shouldn't obj be the callee and thus it's 'this' pointer used?[/quote]
You aren't passing it a this pointer, you are passing it a this reference.

Whether object references will be automatically promoted to object pointers in the case of the 'this' argument to a pointer-to-member-function, is a different matter...

##### Share on other sites
That is true - though in other cases when I pass it a this reference it works fine. The only time it is an issue is when I provide a pointer to a member function of the base class and pass a this reference to an instance of the derived class. But I suppose that goes back to the should it work question, regardless of whether or not in some cases it does work. Edited by Aztral

##### Share on other sites
Yeah I think that should work.

This code compiles and executes fine using the v100 C++ compiler. My question is should this code work? In the 2010 case the code calls vfunc() of the derived class, using obj as the callee, which is what I would expect.

The compiler is instantiating std::bind::operator() which takes const ObjectDerived by value. So you're actually using copy of the obj.

"The arguments to bind are copied or moved, and are never passed by reference unless wrapped in std::ref or std::cref."

##### Share on other sites

Yeah I think that should work.

[quote name='Aztral' timestamp='1348082358' post='4981784']
This code compiles and executes fine using the v100 C++ compiler. My question is should this code work? In the 2010 case the code calls vfunc() of the derived class, using obj as the callee, which is what I would expect.

The compiler is instantiating std::bind::operator() which takes const ObjectDerived by value. So you're actually using copy of the obj.

"The arguments to bind are copied or moved, and are never passed by reference unless wrapped in std::ref or std::cref."
[/quote]

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

I can say for sure in the case of compiler v100 std::bind::operator() isn't making a copy, as this code:

 #include <algorithm> #include <functional> #include <iostream> class ObjectBase { public: virtual void vfunc() const { std::cout << "vfunc base" << std::endl; } }; class ObjectDerived : public ObjectBase { public: void vfunc() const { std::cout << "vfunc derived this = " << this << std::endl; } }; int main(int argc, char *argv[]) { using namespace std::placeholders; const ObjectDerived obj; std::cout << "Addr of obj " << &obj << std::endl; auto func = std::bind(&ObjectBase::vfunc, _1); func(obj); return 0; } 

Outputs:

vfunc derived this = 003EFE20

And this makes sense (to me, at least). If, for example, you are calling std::for_each(container.begin(), container.end(), std::bind(&Class::func, _1)) you would expect Class::func to be called by the actual elements in the container - wouldn't you? Not to mention the performance implications.

That said, I can call func with func(&obj), func(dynamic_cast<ObjectDerived &>(obj)), func(std::move(obj)) so it does seem to be a problem only in the case I am passing the object itself. I'd say this became an issue with the introduction of Rvalue and move semantics but that is not new to 2012, the same language features existed in 2010. Edited by Aztral

##### Share on other sites
Why don't you report it to Microsoft Connect?

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:

[source lang="cpp"]auto func = std::bind(std::mem_fn(&ObjectBase::vfunc), _1);[/source]

So I'd recommend reporting it and seeing what they say. Generally speaking with Microsoft even if they fix it tomorrow it will take you a minimum of the next release of Visual Studio to see it fixed, if not the release after that (they have a habit of releasing beta versions of VS and then closing every bug as "won't fix until vNext"). However with 2012 they are starting out of band releases, so you never know.

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