Sign in to follow this  
Ra

Multiple languages using the same types

Recommended Posts

I've run into a small problem here, which leaves me torn between C and C++. I'm exporting functions written in C++ which accept templated classes ( foo(bar<64>, moo); ). A few different languages actually need to call these functions, C, C++, and eventually some others. The main reason I'm using the templates is to provide a way to use data types that are the same length (32-bit, 16-bit) on all architectures. Now, I could simply use #defines or typedefs and write the entire thing in C, but it's much cleaner and more extensible if I use the templated classes. If I try to call the functions from C I'll need to write new headers with #defines for each size, but it'll work in C++. If I just write the headers in C++ and use templated classes then it won't work with C and I'd probably have to write the C headers anyway (not sure how it'd work with other languages). So... is there a better way to do this? Am I forced to choose between these two methods? Which one should I use?

Share this post


Link to post
Share on other sites
For portability, you need to limit the interfaces you expose. You can do anything internally that you want, but generally the only thing you can use in your exposed interfaces is POD types. This means no classes, templates, namespaces, etc. From the outside, it will look like your code is just plain C but really it can be whatever language you want underneath.

Share this post


Link to post
Share on other sites
I was hoping it wouldn't come to that... I need a good way to *specifically* specify the size of each argument. It makes you wonder why they didn't include additional types that were guaranteed to either be present (and exactly the same) or error out on different architectures.

Share this post


Link to post
Share on other sites
In C, there is stdint.h, which specifies fixed-size integers. In C++, there's Boost's cstdint.hpp, which does essentially the same thing.

Share this post


Link to post
Share on other sites
...wow, where the hell have I been? I totally forgot about stdint.h, thanks. [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this