Eliminate overload wrappers for extern "C" functions?

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

  1. Jared Ahern

    Jared Ahern Guest


    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!

    Jared Ahern, Sep 16, 2010
    1. Advertisements

  2. 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
    1. Advertisements

  3. Jared Ahern

    Jared Ahern Guest

    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
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.