What are potential disadvantages of using extern “C”?

笑着哭i 提交于 2019-12-12 18:51:10

问题


I have read

  • When to use extern "C" in C++?
  • In C++ source, what is the effect of extern "C"?
  • Why do we need extern "C"{ #include <foo.h> } in C++?

However, one question that I have not found an answer to: are there (potentially, future) disadvantages to using extern "C" (e.g., on as many functions as possible)?

To be more specific: Is there any disadvantage in adding extern "C" to functions whose interface only use C functionality; in other words, those that do not use the features listed in @k-five's answer?


回答1:


The disadvantage is that you can only use features in the interface to extern "C" functions that are also available to C functions.

That means:
1. you can't use default values for function arguments,
2. you can't use reference arguments,
3. you can't pass C++ classes by value (including smart pointers),
4. you can't pass enum class arguments,
5. you can't pass bool without converting it to int,
6. you can't overload such functions, and probably more that I can't recall at the moment.



来源:https://stackoverflow.com/questions/46116836/what-are-potential-disadvantages-of-using-extern-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!