Advertisement Jump to content
Sign in to follow this  
Quat

std::bind

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I made the following contrived class to test std::bind:

 

class Database
{
public:
    std::vector<std::unique_ptr<Person>> Data;

    void Sort()
    {
        auto f = std::bind(&Database::CompareAge, this, std::placeholders::_1, std::placeholders::_2);

        std::sort(std::begin(Data), std::end(Data), f);
    }

private:
    bool CompareAge(std::unique_ptr<Person>& x, std::unique_ptr<Person>& y)
    {
        return x->Age < y->Age;
    }
};

 

My question is, how does std::bind work?  It can work with non-member functions and member functions.  I'm assuming it creates a function object behind the scenes, but how does it know to use member function syntax?

 

Share this post


Link to post
Share on other sites
Advertisement

There's a few ways to tackle answering this.

 

I could help you help yourself by telling you that the sourcecode is available on your system, just open the header and have a look for yourself how it works.

However, I can guarantee what you'll find is a ton of templates that are probably not really worth your time to decipher in detail.

 

Bregma's answer may be true of some internal machinery of std::bind, although it's a little misleading as an answer. The std::bind function itself is certainly not a partially specialized template because it's a function template, it can be overloaded and it can be specialized but partial specialization of function templates is not possible.

 

Overall I will just say that there are various ways of dispatching to different code-paths at compile-time based on the types provided as templated arguments. Therefore it knows to use a member-function invoking implementation of std::bind simply because you provided a member-function as the first argument. That argument has a different type than, say, a non-member function and that allows it to pick the appropriate implementation.

Edited by dmatter

Share this post


Link to post
Share on other sites
A long time ago someone provided me with classes that did just this. Possibly before bind existed. I adopted it to take either. Inside the class I had 2 containers. 1 stores the normal function pointer and one stores the class method function pointer. Then you overload the function. The normal function pointer takes different parameters so you then know from that on what container to store it in. Then when you call them you just loop over both containers and call. My implementation was a class and not just a function like std though. I also had different classes for how many params there are and if return type was different.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!