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

This is a discussion on Can I use std::bind to make function pointers instead of std::function objects ? within the C++ Programming forums, part of the General Programming Boards category; Code: #include<functional> typedef void (*foo)(int); typedef std::function<void(int)> bar; class X { public: void fun(int x){}; }; int main() { X ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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 10:05 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    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,053
    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 04: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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    So..back to using global variables, I guess.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 09:50 PM
  4. Replies: 11
    Last Post: 09-22-2006, 05:21 PM
  5. Make an array of function pointers
    By VanJay011379 in forum C Programming
    Replies: 2
    Last Post: 02-09-2003, 05:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21