Eliminate overload wrappers for extern "C" functions?

J

Jared Ahern

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
 
J

Johannes Schaub (litb)

Jared said:
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.
 
J

Jared Ahern

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!
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top