Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Eliminate overload wrappers for extern "C" functions?

Reply
Thread Tools

Eliminate overload wrappers for extern "C" functions?

 
 
Jared Ahern
Guest
Posts: n/a
 
      09-16-2010
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
 
Reply With Quote
 
 
 
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      09-16-2010
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.
 
Reply With Quote
 
 
 
 
Jared Ahern
Guest
Posts: n/a
 
      09-16-2010
On Sep 16, 4:41*pm, "Johannes Schaub (litb)" <(E-Mail Removed)>
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
extern const char * vs. extern const char []http://tinyurl.com/47e3k Thomas Matthews C++ 5 08-02-2004 10:36 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM
Managed wrappers in ASP.NET Session mirek ASP .Net 5 10-21-2003 04:52 AM



Advertisments