views:

21

answers:

1

I'm writing a light Unicode library, but I'm stuck as to whether to write it for C, or C++? C++ has the benefit of operator overloads and better class handling, but many programs are written in plain C and it would seem nice to be compatible with C. How should I decide what language to write this for?

By the way, I know that there are many of these libraries around; I'm writing the library merely as an exercise, though I'll probably use it in my programs.

+1  A: 

Easy - write it in C and have C++ wrappers. This makes both the C and C++ consumers happy, and if written correctly there's little overhead in wrapping C with C++. Be sure to follow sane models for re-entrant code - for example, having init and destroy functions handling struct pointers in C would correspond to constructors and destructors of a class in C++, where the struct pointer corresponds to the this pointer.

Reinderien
Does this mean that I'll write the C base library with something such as `ustrcat` (for example), but in the C++ wrapper, add an operator overload that calls `ustrcat` internally on the strings?
Delan Azabani
If I'm interpreting this correctly, ustrcat is a unicode string concatenation function that in C would accept two string pointers. In C++ you might wrap this as a unicode string class having a concatenate member (or a += operator) whose operand is a reference to another instance of the unicode string class. In that case, yes, the concatenation member function would internally call `ustrcat`.
Reinderien
Yes, that is what I mean. Would that be the appropriate way to go about it?
Delan Azabani
Exactly. Additionally, note that you can issue the libraries in one of two ways - either make a standalone C library, make a C++ library that depends on it and ship one or both; or make a C library, and for the C++ library recompile so that the C++ members can inline the C functions, so that the C++ library is also standalone, then ship either one lib or the other.
Reinderien