Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

How Overloading Interacts With C Compatibility

April 11, 2014

Last week, I described an early version of how C++ handled overloading: A C++ function either would act like a C function or would be overloaded. The programmer identified the latter case with an overload declaration; if such a declaration appears at all, it must appear in every translation unit that uses that particular function name.

This approach made life easier for implementors in an important way: An implementation could assume that a function that was not declared as overloaded would be compatible with a C function with the same name. However, the approach had the disadvantage that every C++ programmer who defined a function had to know about every function with that name in every other translation unit, even if those other functions were unrelated. Consider:

 
               // Translation unit 1
               void foo(int n) { }
 
               // Translation unit 2
               void foo() { }

Here we have two functions, each named foo, that should be able to coexisist with each other. After all, if these functions both appeared in a single translation unit, they would be considered as overloading the name foo (provided that there was an overload declaration), so surely they should be able to appear in two separate translation units.

In practice, life was not that simple. If a C++ function named foo is to be compatible with a C function named foo, and computability implies giving the same name (i.e., foo) to the linker (in case a C function wanted to use it), then it is hard to understand how both of these foo functions can coexist with each other when we run them under a C-compatible linker. To put it another way: The authors of the two translation units above may well not have known of each others' existence, and almost surely did not care that foo was effectively overloaded. Nevertheless, it is reasonable to argue that each of them should have put an overload declaration into their respective translation units, in order to cater to the possibility of someone else using the same name in a different translation unit.

This line of reasoning leads to the conclusion that the language should really be defined so that functions are overloadable by default; a programmer who wants to be able to call a C function from C++ (or define a function in C++ that a C function can call) should have to define that function explicitly as being compatible with C. In effect, C compatibility becomes an attribute of a function that is separate from its argument or return types, name, and so on.

Of course, as soon as we have the notion that a C++ function might have a "compatible with a different language" attribute, new questions arise:

  • Should it be possible to tell a C++ compiler that a function is written in a language other than C or C++? If so, how is the list of permissible languages determined?
  • Is the language in which a function is written part of the function's type as far as C++ is concerned? Is it possible to overload a function based on that attribute?
  • Should it be possible to say that data, not just functions, are compatible with how those data are stored in another language?

These questions, and others, are all consequences of the seemingly trivial decision to do away with an explicit overload declaration. Until then, there were really only two kinds of C++ functions: ordinary functions and overloaded functions. As a result, ordinary functions had to be compatible with C because overloaded functions could not be C-compatible, and everything else fell into place.

Merely reversing the default — saying that ordinary functions were overloaded C++ functions — made it possible to ask first how to say that a C++ function was to be C-compatible, and then to ask whether there were any other compatibility options. And once it becomes possible to ask such questions, sooner or later it becomes necessary to answer them.

In short, this is one of these situations in which a seemingly simple language extension opens the possibility of using the language in ways that did not exist before. Catering to these ways of using the language, in turn, required answers to a series of new questions. Once again, we see how the social properties of a programming language's user community affect the technical details of the language itself.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video