Eliminate overload wrappers for extern "C" functions?

Discussion in 'C++' started by Jared Ahern, Sep 16, 2010.

  1. Jared Ahern

    Jared Ahern Guest

    Hello,

    I have a C library with several functions that do the same operation
    on different input types. These functions can be used in C++ with
    'extern "C"'. It would be nice to overload them; here is the way I
    presently go about it:

    extern "C" int foo_int(int const &);
    extern "C" int foo_float(float const &);
    int foo(int const & a) { return foo_int(a); };
    int foo(float const & a) { return foo_float(a); };

    Is there a way to accomplish this without actually creating the
    wrapper functions? (Perhaps a way to create portable name-mangled
    aliases?) The ability to access the original C names from C++
    afterward is not required. My concern is not so much the amount of
    code but rather the nested function calls, which could possibly
    contribute to a performance hit.

    Sorry if this has been addressed before, I didn't happen to find it!

    Thanks,
    Jared
    Jared Ahern, Sep 16, 2010
    #1
    1. Advertising

  2. Jared Ahern wrote:

    > Hello,
    >
    > I have a C library with several functions that do the same operation
    > on different input types. These functions can be used in C++ with
    > 'extern "C"'. It would be nice to overload them; here is the way I
    > presently go about it:
    >
    > extern "C" int foo_int(int const &);
    > extern "C" int foo_float(float const &);
    > int foo(int const & a) { return foo_int(a); };
    > int foo(float const & a) { return foo_float(a); };
    >
    > Is there a way to accomplish this without actually creating the
    > wrapper functions? (Perhaps a way to create portable name-mangled
    > aliases?) The ability to access the original C names from C++
    > afterward is not required. My concern is not so much the amount of
    > code but rather the nested function calls, which could possibly
    > contribute to a performance hit.
    >


    If "foo" is visible to the compiler it can (and, i bet, most probably will)
    inline this. Make "foo" inline functions and put them into headers.

    You could abuse surrogate call functions to get rid of the wrappers, but I
    don't think the work pays off.
    Johannes Schaub (litb), Sep 16, 2010
    #2
    1. Advertising

  3. Jared Ahern

    Jared Ahern Guest

    On Sep 16, 4:41 pm, "Johannes Schaub (litb)" <>
    wrote:
    > Jared Ahern wrote:
    > > Hello,

    >
    > > I have a C library with several functions that do the same operation
    > > on different input types.  These functions can be used in C++ with
    > > 'extern "C"'.  It would be nice to overload them; here is the way I
    > > presently go about it:

    >
    > > extern "C" int foo_int(int const &);
    > > extern "C" int foo_float(float const &);
    > > int foo(int const & a) { return foo_int(a); };
    > > int foo(float const & a) { return foo_float(a); };

    >
    > > Is there a way to accomplish this without actually creating the
    > > wrapper functions?  (Perhaps a way to create portable name-mangled
    > > aliases?)  The ability to access the original C names from C++
    > > afterward is not required.  My concern is not so much the amount of
    > > code but rather the nested function calls, which could possibly
    > > contribute to a performance hit.

    >
    > If "foo" is visible to the compiler it can (and, i bet, most probably will)
    > inline this. Make "foo" inline functions and put them into headers.
    >
    > You could abuse surrogate call functions to get rid of the wrappers, but I
    > don't think the work pays off.


    Sounds good. I just wasn't sure if there was another way to do this
    association of which I was not aware. Thanks!
    Jared Ahern, Sep 16, 2010
    #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. Piotre Ugrumov
    Replies:
    3
    Views:
    361
    Nick Hounsome
    Jan 25, 2004
  2. Thomas Matthews
    Replies:
    5
    Views:
    2,367
    tom_usenet
    Aug 2, 2004
  3. Matthew Wilson
    Replies:
    6
    Views:
    288
    Bjoern Schliessmann
    Sep 27, 2007
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    234
    Sherm Pendley
    Oct 11, 2004
  5. Andre
    Replies:
    5
    Views:
    517
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page