Thread: Can I use std::bind to make function pointers instead of std::function objects ?

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

    Can I use std::bind to make function pointers instead of std::function objects ?

    Code:
    #include<functional>
    
    typedef void (*foo)(int);
    typedef std::function<void(int)> bar;
    
    class X
    {
    public:
        void fun(int x){};
    };
    
    int main()
    {
        X x;
        bar b=std::bind(&X::fun,&x,std::placeholders::_1); //This works
    //     foo f=std::bind(&X::fun,&x,std::placeholders::_1); //This doesn't
    }
    But I need the to pass the function to a posix function (signal) defined to take function pointers of type foo.
    Is there a way I can pass a member function (along with the object) to something defined to take function pointers only?

    EDIT:
    There turns out to be a member function of std::function that appears to do what I need, but after successfully compiling, promptly segfaults.
    Code:
    #include<functional>
    #include<iostream>
    typedef void (*foo)(int);
    typedef std::function<void(int)> bar;
    
    class X
    {
    public:
        X(int x):y(x){};
        void fun(int x){std::cout<<x<<y;};
    private:
        int y;
    };
    
    int main()
    {
        X x(4);
        bar b=std::bind(&X::fun,x,std::placeholders::_1);
        foo f=*b.target<foo>();
        f(5);
    }
    Last edited by manasij7479; 12-10-2013 at 11:05 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I know lambdas are convertible to function pointer if and only if they have no state. This makes sense, since a function pointer cannot store state. Likewise: a member function needs an instance. How are you going to encode that instance into a function pointer?
    I don't know about std::bind and std::function, though.
    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
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The segfault problem is that foo is not the target type, and therefore target returns 0.

    The target type is not a function pointer but the type specified by the result of bind. So you could do:
    Code:
    b.target<decltype( std::bind(&X::fun,x,std::placeholders::_1) )>()
    But you can't put the result in a regular function pointer.
    Last edited by King Mir; 12-10-2013 at 05:36 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    So..back to using global variables, I guess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-14-2011, 12:38 PM
  2. Replies: 1
    Last Post: 05-24-2011, 06:36 PM
  3. 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
  4. Replies: 11
    Last Post: 09-22-2006, 05:21 PM