问题
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