Monday, July 30, 2018

What exactly do I lose when using extern "C" in C++?




I'm trying to develop a dynamic library in C++ to be called by an existing program written in IDL (Interactive Data Language). I know that I need to use extern "C" to disable name mangling so that IDL can call the functions it needs (the rest of the calling mechanism is pretty straightforward).



However, I'm always hesitant to use features of a language that I don't fully understand, so my question is this: What features of C++ do I lose by reverting to C linkage, if any? Namespaces would be an obvious one, I think, but does it completely disable all the other nice features of C++ too? Can I still use the C++ STL, and all the various language features (especially C++11 ones) that I've come to rely on? Or am I stuck essentially coding in C?


Answer



The only thing that gets dropped is name mangling of externally visible names. Function overloading by parameter types, as well as by parameter count, stop working as the result. Essentially, name resolution during the linking phase goes back to the plain old C mode (i.e. one name - one entry).



As far as the internals of your implementation go, you can continue using the standard library and all the other nice features of C++11. Only the names of externally visible entities get changed by extern C.



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...