• Create Account

## std::bind - Should this code work?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Aztral  Members

1129
Like
0Likes
Like

Posted 19 September 2012 - 01:19 PM

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, 19 September 2012 - 01:22 PM.

### #2swiftcoder  Senior Moderators

17831
Like
0Likes
Like

Posted 19 September 2012 - 01:27 PM

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, 19 September 2012 - 01:27 PM.

Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]

### #3Bregma  Members

8322
Like
0Likes
Like

Posted 19 September 2012 - 01:44 PM

Data point: your code compiles and works just peachy on GCC 4.7.
Stephen M. Webb
Professional Free Software Developer

### #4Servant of the Lord  Members

33572
Like
0Likes
Like

Posted 19 September 2012 - 01:48 PM

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, 19 September 2012 - 01:49 PM.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #5Aztral  Members

1129
Like
0Likes
Like

Posted 19 September 2012 - 02:01 PM

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, 19 September 2012 - 02:01 PM.

### #6swiftcoder  Senior Moderators

17831
Like
1Likes
Like

Posted 19 September 2012 - 02:30 PM

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?

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

Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]

### #7Aztral  Members

1129
Like
0Likes
Like

Posted 19 September 2012 - 02:49 PM

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, 19 September 2012 - 02:49 PM.

### #8Codarki  Members

462
Like
0Likes
Like

Posted 20 September 2012 - 02:18 AM

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."

### #9Aztral  Members

1129
Like
0Likes
Like

Posted 20 September 2012 - 11:11 AM

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."

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, 20 September 2012 - 11:12 AM.

### #10gekko  Members

478
Like
0Likes
Like

Posted 21 September 2012 - 12:46 AM

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, 21 September 2012 - 08:53 AM.

-- gekko

### #11Aztral  Members

1129
Like
0Likes
Like

Posted 21 September 2012 - 10:01 AM

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.

### #12Codarki  Members

462
Like
0Likes
Like

Posted 21 September 2012 - 02:39 PM

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

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?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.