使用外部"C"的潜在缺点是什么? [英] What are potential disadvantages of using extern "C"?
问题描述
我已阅读
- 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++?
但是,我没有找到一个答案:使用extern "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)?
更具体地说:在接口仅使用C功能的函数中添加extern "C"
是否有任何不利之处;换句话说,那些不使用@ k-five答案中列出的功能的人?
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?
推荐答案
缺点是您只能在接口中使用extern "C"
函数的功能,这些功能也可供C函数使用.
The disadvantage is that you can only use features in the interface to extern "C"
functions that are also available to C functions.
这意味着:
1.您不能使用默认值作为函数参数,
2.您不能使用引用参数,
3.您不能按值(包括智能指针)传递C ++类,
4.您不能传递enum class
参数,
5.您不能通过bool
而不将其转换为int
,
6.您不能重载此类功能,而且可能无法重载我目前无法回忆起的内容.
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.
这篇关于使用外部"C"的潜在缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!