• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Aztral

std::bind - Should this code work?

11 posts in this topic

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:

[CODE]
#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;
}
[/CODE]

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:

[CODE]
const ObjectBase &operator*() const { return *this; }
[/CODE]

When I do so I can compile and run.

This code compiles and executes fine using the v100 C++ compiler. My question is [i]should[/i] 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:

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

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
0

Share this post


Link to post
Share on other sites
[quote name='Aztral' timestamp='1348082358' post='4981784']
My question is should this code work?[/quote]
My gut feeling is no. It is called the 'this [b]pointer[/b]' for a reason.

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

Share this post


Link to post
Share on other sites
std::function can [url="http://www.stroustrup.com/C++11FAQ.html#std-function"]wrap member functions[/url] as (imitating) regular functions; then maybe you can std::bind that?

Code example copy+pasted from Bjarne's webpage:
[code]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)[/code] Edited by Servant of the Lord
0

Share this post


Link to post
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.
[/quote]

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

[quote name='Bregma' timestamp='1348083874' post='4981794']
Data point: your code compiles and works just peachy on GCC 4.7
[/quote]

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

[quote name='Servant of the Lord' timestamp='1348084137' post='4981796']
std::function can wrap member functions as (imitating) regular functions; then maybe you can std::bind that?
[/quote]

This also compiles in 2010 and fails in 2012. Interestingly, the sample code at [url="http://en.cppreference.com/w/cpp/utility/functional/function"]http://en.cppreferen...tional/function[/url] 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 [i]should[/i] 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
0

Share this post


Link to post
Share on other sites
[quote name='Aztral' timestamp='1348084872' post='4981802']
[quote name='swiftcoder' timestamp='1348082846' post='4981789']
My gut feeling is no. It is called the 'this pointer' for a reason.[/quote]
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 [b]pointer[/b], you are passing it a this [b]reference[/b].

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

Share this post


Link to post
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
0

Share this post


Link to post
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 [i]should[/i] 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.
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='Codarki' timestamp='1348129080' post='4981961']
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 [i]should[/i] 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.
[/quote]

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:

[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;
}
[/code]

Outputs:

Addr of obj 003EFE20
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
0

Share this post


Link to post
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
0

Share this post


Link to post
Share on other sites
[quote name='gekko' timestamp='1348209993' post='4982255']
Why don't you report it to Microsoft Connect?
[/quote]

[quote name='gekko' timestamp='1348209993' post='4982255']
And if you do report it, would you mind posting the link? I wouldn't mind tracking it to see what they say.
[/quote]

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

[quote name='gekko' timestamp='1348209993' post='4982255']
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
[/quote]

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:

[CODE]
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);
[/CODE]

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 [i]wouldn't[/i] work, even if it's not technically the best solution.
0

Share this post


Link to post
Share on other sites
[quote name='Aztral' timestamp='1348161072' post='4982091']
I believe your quote is referring to the arguments actually passed to the std::bind call.
[/quote]

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:
[quote]
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?
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0