4

I have read

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?

bers
  • 4,817
  • 2
  • 40
  • 59
  • 1
    `extern "C"` is there for serving a purpose . Your question's answer depends on what context you're assuming its pros and cons. – P0W Sep 08 '17 at 12:43
  • The disadvantage would be that you cannot use C++ stuff like overloading or member functions. And you have to write more code that doesn't really do anything. – nwp Sep 08 '17 at 12:44
  • Despite its name, this is not related to the C language/standard. – too honest for this site Sep 08 '17 at 12:47
  • Does it mention how to call C-ABI funtions without it? – too honest for this site Sep 08 '17 at 12:49
  • 3
    What do you mean with "disadvantage"? If you buy a bike is it a disadvantage you cannot tranport a grande piano with it? That's just not what a bike is for! Don't use a screwdriver to hammer a nail. – too honest for this site Sep 08 '17 at 12:54
  • Detecting stack corruption caused by changed declarations at link time is quite superior to having to debug it at runtime. – Hans Passant Sep 08 '17 at 13:08
  • @Olaf a disadvantage, to me, is anything that one could do without `extern "C"`, but won't be able to with it. I'm sorry I cannot narrow it down further. I was considering suggesting to someone else to use `extern "C"` by default wherever possible in their C++ code to make it easier for me to use that interface in C. – bers Sep 09 '17 at 08:35
  • @bers: Read my comment **carefully** again. Then think about it! – too honest for this site Sep 09 '17 at 13:02
  • @Olaf: Thanks for the advice, I have done that, and my comment above remains the same. Let me explain: a "disadvantage" is "an unfavourable circumstance or condition that reduces the chances of success or effectiveness" ; "reducing" implying a relation to a situation with said circumstance or condition. Now, not being a able to transport a piano with a bike is not a disadvantage in relation to not having a bike, you are right; assuming that I was not able to transport a piano without the bike, either. – bers Sep 09 '17 at 16:12
  • However, my situation is more like having a truck able to transport a piano, and now I downgrade my truck to a bike for financial or environmental reasons. While I myself may be entirely happy with that decision (for example, because I don't have a piano), it is still a disadvantage of a bike in relation to a truck of not being able to transport a piano. Well, it may not be an _effective_ disadvantage now, but it may be in the future; it certainly makes buying a piano more difficult for me. To make you more happy, I have added the work "potential" to the question. – bers Sep 09 '17 at 16:15
  • As a grande piano typically has wheels, it would actually be easier to transport it by foot than bike. [Not that it is really easy](https://www.youtube.com/watch?v=N3CuXnB928g). But yeah, concentrate on a single part of my comment! Anyway, I don't see how to make you understand the point (apparently other did). – too honest for this site Sep 09 '17 at 16:18

1 Answers1

6

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.

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • 1
    I don't see that a dsadvantage, because that's what the construct is meant for. (btw: C **has** a boolean type; not useful to convert to `int`). – too honest for this site Sep 08 '17 at 12:52
  • 1
    @Olaf It is a disadvantage to wrap everything in `extern "C"`, because then you are writing *neither* modern C *nor* modern C++ – Caleth Sep 08 '17 at 12:56
  • This answer is misleading. You **can** use C++ features **inside** `extern "C"` functions. What you **can't** do, is use C++ features in the declaration.. But there's no problem using `std::vector` in the implementation. It's unspecified whether C++ `bool` matches C99 `_Boolean`, but his is reasonable to expect. – MSalters Sep 08 '17 at 13:07
  • That's not a disadvantage, but not what `extern "C"` is meant for. If you want to write C code, use a C compiler, you cannot write C code in a C++ compiler anyway - no matter if wrapped in `extern "C"` or not. The fact you cannot fly with a car is not a disadvantage of a street car, it is just a property. Don't use a screwdriver to hammer a nail. – too honest for this site Sep 08 '17 at 13:34
  • @MSalters: It is `_Bool` in C ;-) – too honest for this site Sep 08 '17 at 13:35
  • @JesperJuhl `you can't pass C++ classes by value` does this apply to *all* classes? Even standard layout ones? – eerorika Sep 08 '17 at 13:40