From 1814224832816438637
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,bb04047befc790b4
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2001-02-24 16:55:11 PST
Path: supernews.google.com!sn-xit-02!supernews.com!news.gv.tsc.tdk.com!news.iac.net!news-out.cwix.com!newsfeed.cwix.com!news.maxwell.syr.edu!dispose.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail
From: "James Kuyper Jr." <kuyper@wizard.net>
Newsgroups: comp.std.c++
Subject: Re: empty parameter list and extern "C"
Date: Sun, 25 Feb 2001 00:48:52 GMT
Organization: Not Enough
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <3A970F80.57D6AD5E@wizard.net>
References: <3a91a017.213629713@news.nikoma.de> <h9f99tc98a3gp6q4a2e5n47imt44fr8b5k@4ax.com> <3a968230.533654664@news.nikoma.de>
X-Trace: mail2news.demon.co.uk 983062145 mail2news:10790 mail2news mail2news.demon.co.uk
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Accept-Language: en,es,de,ru
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 72
Xref: supernews.google.com comp.std.c++:3771

Martin Aupperle wrote:
> 
> On Thu, 22 Feb 2001 19:32:10 GMT, Jack Klein <jackklein@spamcop.net>
> wrote:
> 
> >
> >Understand that C++ does not guarantee interoperability with code
> >compiled by a C compiler, or even by the same compiler in its C mode,
> >if it has one.  The intent is to provide this capability if possible,
> >but all that extern "C" is guaranteed to do is provide C linkage.
> >
> I understand. So, please look at the following scenario:
> 
> 1.)  I have a very old and mean C-Library (i.e. one that is compiled
> with a C-compiler). It exposes a header file foo.h that declares a
> function f like this:
> 
>         void f();
> 
> 2.) Since it is a C-Library, I conclude that f can (syntactically) be
> called with arbitrary arguments. Any C-Program that includes foo.h
> should be able to do so.

Syntactically, yes. Semantically, no. According to the current C
standard, it's undefined behavior to call f() with arguments whose
number and promoted types are incompatible with those of the parameters
in the definition of f().

> 3.) But I have a C++ program and want to use the library. Therefore I
> say
> 
>         extern "C"
>         {
>           #include "foo.h"
>         }
> 
> Now I have a function  f declared and can use it. BUT NOT with
> arbitrary parameters!
> 
> 4.) It is also not possible to declare f myself, as for example like
> this

If foo() does in fact take no arguments, you should change "foo.h" to
declare it as

	void f(void);

This is legal in C++, and means the same thing as in C. If foo() does in
fact take a fixed number of arguments, declare it with that number of
arguments. If the code using foo() takes advantage of the fact that C
used to allow discrepances between the arguments in a function call and
the parameters of the corresponding function definition, you may be out
of luck, since that's equally illegal in modern C++ as in modern C.
Offhand I see no easy way to trick a modern compiler into cooperating
with such a discrepancy. You'd have to find a compiler with a "k&r"
option that produces modules linkable to code compiled without that
option.

If "foo.h" is inaccessible, you will either have to write your own
replacement for it with a different name, or write suitably defined
wrapper functions, as you've already figured out. One thing you don't
have to worry about is the impossibility of wrapping a function defined
with an '...'. Such a function cannot be legally called using a
declaration as "void f();", neither in C nor in C++.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]



