OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:43 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Calling a mangled template in C
PostPosted: Fri Aug 12, 2016 8:26 pm 
Offline

Joined: Thu Nov 08, 2012 2:59 am
Posts: 15
I am wanting to develop an OS using a mix of ASM, C, and C++. However, I am unsure of how I would call a C++ template function from C. I have included an example function that I would want to use below:

Code:
template <class T>
T *memcpy(T *dest, T *src, int count)
{
   int i;
   for(i=0; i < count; i++)
   {
      *dest = *src;
   }
   return dest;
}


Can anyone either tell me how I would go about calling this function in C, or point me some where that can explain this?
P.S. I am aware of name mangling.


Top
 Profile  
 
 Post subject: Re: Calling a mangled template in C
PostPosted: Fri Aug 12, 2016 9:03 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
http://stackoverflow.com/questions/2152 ... nstantiate ?


Top
 Profile  
 
 Post subject: Re: Calling a mangled template in C
PostPosted: Fri Aug 12, 2016 10:15 pm 
Offline

Joined: Thu Nov 08, 2012 2:59 am
Posts: 15
Not quite what I meant. Though I did just realize that I named this topic badly. What I meant was, if I know the mangled name of a function, how do I pass arguments when I am calling the fuction from C?


Top
 Profile  
 
 Post subject: Re: Calling a mangled template in C
PostPosted: Sat Aug 13, 2016 12:12 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
I'd expect, the same way as usual, except for name mangling. You can always look at the (dis)assembly of the instantiated function.


Top
 Profile  
 
 Post subject: Re: Calling a mangled template in C
PostPosted: Sat Aug 13, 2016 2:27 am 
Offline

Joined: Thu Nov 08, 2012 2:59 am
Posts: 15
I would expect the same as well. However, I do not know how C language function call really looks in assembly, though I could probably find out quickly if I wanted to. In any case, I realized that it doesn't really matter as all of my non-assembly source files will be run through g++, so that would take care of the whole name mangling problem on it's own. Thanks for the help though.


Top
 Profile  
 
 Post subject: Re: Calling a mangled template in C
PostPosted: Sat Aug 13, 2016 3:12 am 
Offline
Member
Member

Joined: Thu Mar 25, 2010 11:26 pm
Posts: 1801
Location: Melbourne, Australia
The best way, in my opinion is to write your 'c' in a cpp file and compile it with g++. Then it all just works.

_________________
If a trainstation is where trains stop, what is a workstation ?


Top
 Profile  
 
 Post subject: Re: Calling a mangled template in C
PostPosted: Sat Aug 13, 2016 6:02 am 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
you can't really do that because functions with templates will always be inlined, possibly unless you have a specialized version.


Top
 Profile  
 
 Post subject: Re: Calling a mangled template in C
PostPosted: Sat Aug 13, 2016 12:52 pm 
Offline

Joined: Thu Nov 08, 2012 2:59 am
Posts: 15
gerryg400 wrote:
The best way, in my opinion is to write your 'c' in a cpp file and compile it with g++. Then it all just works.


I figured that would be the best way. If I fo it all through g++, the name mangling problem takes care of itself. I just have to be careful that I make any function that would be called in my assembly code is linked in c-style.

Though I have to ask, if I have a c-style function, and I have it call a c++ function, does the called function have to be explicitly stated as a c++ function?

I.e.
Code:
extern "C" void print (string x)
{
std::cout << x << std::endl;
return;
}


Would the cout and endl functions be called c-style or c++ style?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 37 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group