Thread: Is std::bind exactly equivalent to a function object If I create a similar one ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Is std::bind exactly equivalent to a function object If I create a similar one ?

    <Though the subject line may be a bit misleading, bear with me a little>

    I was reading up on pthread `s and then found out std::thread. It ...seems wonderful.

    So, are the following versions of the snippet exactly equivalent?

    Code:
    class X
    {
        public:
        std::string foo;
        void operator()(){std::cout<<foo<<std::endl;}
    };
    int main()
    {
        X bar;
        cin>>bar.foo;
        thread t(bar);
        t.join();
        return 0;
    }
    Code:
    void foo(std::string bar)
    {
        std::cout<<bar<<std::endl;
    }
    int main()
    {
        string bar;
        cin>>bar;
        thread t(bind(foo,bar));
        t.join();
        return 0;
    }
    Or is there some hidden difference I should know about ?

    [Also, I remember seeing a thread here in which someone posted about a bug in gcc's implementation of std::thread. I can't find it now. Anyone remember what it was about?]

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They will work the same, yes.
    Note that boost::bind stores a copy of bar unless you wrap it in std::ref. Import if you pass a reference to a function which you want it to modify.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    They will work the same, yes.
    Note that boost::bind stores a copy of bar unless you wrap it in std::ref. Import if you pass a reference to a function which you want it to modify.
    By "work" do you mean they give the same output? (They do, as I made these snippets with that in mind.)
    Or as I read here that bind creates f-objects(not just variables) as an intermediate? (Though I suspect that it is implementation defined. )
    Last edited by manasij7479; 08-14-2011 at 07:56 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, they will give the same output.
    boost::bind creates a functor object which it returns, and when invoked, calls the desired function with all the bound parameters, so it is an itermediate step.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tr1::function, tr1::bind and class member functions
    By leeor_net in forum C++ Programming
    Replies: 1
    Last Post: 02-08-2011, 10:50 PM
  2. Is there anyway to create a 24-bit object?
    By maxhavoc in forum C++ Programming
    Replies: 10
    Last Post: 06-15-2006, 07:28 AM
  3. function similar to strpos() ?
    By willc0de4food in forum C Programming
    Replies: 18
    Last Post: 10-08-2005, 04:13 PM
  4. using switch to create an object
    By djardim in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2005, 08:08 PM
  5. how to create a tree of object
    By winsonlee in forum C++ Programming
    Replies: 1
    Last Post: 08-25-2004, 01:20 AM