So I've seen some C libraries written so that they compile without any changes with a C++ compiler, for example Lua.
What are some advantages and disadvantages of doing this?
A few basic ones I see are:
Advantages:
- library can be used with any C or C++ project with no extra effort (without using
extern "C"
for example).
Disadvantages:
- Harder to maintain because you need it compile as both (involves things like extra casts which can hide bugs)
- Either limit yourself to C/C++ (sacrificing things like flexible array members), or end up with non-standard C++.
But I'm sure there's more to consider than just the above advantages and disadvantages. What sort of cases would doing this be appropriate, instead of using either pure C or C++ for the library?
Best Answer
Well, there are many disadvantages:
I wonder if there is any good documentation for that? Especially as every combination of C version and C++ version is a different challenge.
extern "C"
-block anyway.Why not simply write it in whatever language you want, featuring a C-interface, and optionally adding a C++ facade on top of it for ease of use?