[Fwd: inline functions and function pointers]

Discussion in 'C++' started by Balbir Singh, Jul 13, 2004.

  1. Balbir Singh

    Balbir Singh Guest

    I was wondering if a function pointer pointing to an inline
    function, will actually expand "inline" when the function pointer
    is invoked.



    #include <iostream>
    #include <vector>
    using namespace std;

    const int MAX_FP_SIZE = 10;
    typedef void (*fp)(int& i);
    vector<fp> vfptr(0);

    inline void incr_callback(int& i)
    {
    i++;
    cout << "i is " << i << endl;
    }

    inline void decr_callback(int& i)
    {
    i--;
    cout << "i is " << i << endl;
    }

    void
    register_callback(fp fptr)
    {
    vfptr.push_back(fptr);
    }

    void
    schedule_callbacks(void)
    {
    int i = 0;
    vector<fp>::iterator iter;
    fp tmp_fp;
    cout << "i is " << i << endl;

    for (iter = vfptr.begin(); iter != vfptr.end(); iter++) {
    tmp_fp = *iter;
    tmp_fp(i);
    }
    }

    int
    main(void)
    {
    register_callback(incr_callback);
    register_callback(decr_callback);
    schedule_callbacks();
    }


    In the piece of code above, I would actually like tmp_fp(i) to expand
    inline. I understand that such optimization is hard to achieve, but
    are there any compilers out there that can do this?

    TIA,
    Balbir

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Balbir Singh, Jul 13, 2004
    #1
    1. Advertising

  2. Balbir Singh wrote:

    > I was wondering if a function pointer pointing to an inline
    > function, will actually expand "inline" when the function pointer
    > is invoked.
    >
    >
    >
    > #include <iostream>
    > #include <vector>
    > using namespace std;
    >
    > const int MAX_FP_SIZE = 10;
    > typedef void (*fp)(int& i);
    > vector<fp> vfptr(0);
    >
    > inline void incr_callback(int& i)
    > {
    > i++;
    > cout << "i is " << i << endl;
    > }
    >
    > inline void decr_callback(int& i)
    > {
    > i--;
    > cout << "i is " << i << endl;
    > }
    >
    > void
    > register_callback(fp fptr)
    > {
    > vfptr.push_back(fptr);
    > }
    >
    > void
    > schedule_callbacks(void)
    > {
    > int i = 0;
    > vector<fp>::iterator iter;
    > fp tmp_fp;
    > cout << "i is " << i << endl;
    >
    > for (iter = vfptr.begin(); iter != vfptr.end(); iter++) {
    > tmp_fp = *iter;
    > tmp_fp(i);
    > }
    > }
    >
    > int
    > main(void)
    > {
    > register_callback(incr_callback);
    > register_callback(decr_callback);
    > schedule_callbacks();
    > }
    >
    >
    > In the piece of code above, I would actually like tmp_fp(i) to expand
    > inline.


    Why?

    > I understand that such optimization is hard to achieve, but
    > are there any compilers out there that can do this?


    I seriously doubt it. I doubt it is even possible at all. Maybe for
    trivial and isolated cases like this one where all definitions are in
    the same translation unit it might be _theoretically_ possible. But then
    again for trivial examples like this you might as well call
    incr_callback() and decr_callback() from schedule_callbacks() directly.
    For more dynamic cases when it is not known at compile time which
    functions need to be called by schedule_callbacks(), how could the
    compiler inline code if it does not know in advance which functions need
    to be executed?

    Even if it is possible, it appears to be an extremely difficult
    optimization that gains close to nothing.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Peter van Merkerk, Jul 14, 2004
    #2
    1. Advertising

  3. Peter van Merkerk wrote:

    > I seriously doubt it. I doubt it is even possible at all. Maybe for
    > trivial and isolated cases like this one where all definitions are in
    > the same translation unit it might be _theoretically_ possible. But then
    > again for trivial examples like this you might as well call
    > incr_callback() and decr_callback() from schedule_callbacks() directly.
    > For more dynamic cases when it is not known at compile time which
    > functions need to be called by schedule_callbacks(), how could the
    > compiler inline code if it does not know in advance which functions need
    > to be executed?
    >
    > Even if it is possible, it appears to be an extremely difficult
    > optimization that gains close to nothing.



    In general inlining of functions when they are called through pointers
    is difficult to be done.

    If you want to do this you have to use static member functions inside a
    class.


    Example:


    class whatever
    {
    public:
    inline static void something(int x)
    {
    // ...
    }
    };


    template<class T>
    void test()
    {
    int i=1;

    // ...

    // void whatever::something(int) is inlined here
    T::something(i);
    }



    int main()
    {
    test<whatever>();
    }






    Regards,

    Ioannis Vranos

    http://www23.brinkster.com/noicys

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Ioannis Vranos, Jul 14, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    460
  2. Daniel Vallstrom
    Replies:
    2
    Views:
    1,895
    Kevin Bracey
    Nov 21, 2003
  3. Davide Carboni
    Replies:
    0
    Views:
    363
    Davide Carboni
    Dec 18, 2003
  4. TGOS
    Replies:
    3
    Views:
    379
    Kevin Bracey
    Feb 28, 2005
  5. Rahul
    Replies:
    3
    Views:
    457
    James Kanze
    Feb 28, 2008
Loading...

Share This Page