Topic: Extern "C" and parameters
Author: jrwinter@mtu.edu (Jeffrey R. Winters)
Date: 1997/05/14 Raw View
My question is best illustrated by an example.
Is the following legal?
extern "C"
{
//foo can take any number of 'C' parameters
// of any type.
int foo();
}
void bar(int i)
{
foo(i);
}
Or am I expected to permute all the different flavors that I want to
call from C++???
BTW - Two compilers I'm using both treat this differently (one gives an
error and the other accepts it)...I'm trying to determine which vender
to notify.
Thanks,
Jeff Winters
jrwinter@mtu.edu
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Steve Clamage <stephen.clamage@eng.sun.com>
Date: 1997/05/15 Raw View
Jeffrey R. Winters wrote:
>
> My question is best illustrated by an example.
> Is the following legal?
No.
> extern "C"
> {
> //foo can take any number of 'C' parameters
> // of any type.
> int foo();
The 'extern "C"' just means the function has C linkage.
It does not create a loophole in the C++ type system. This
declaration of foo means it does not accept any parameters,
no matter what linkage you specify.
> Or am I expected to permute all the different flavors that I want to
> call from C++???
Function foo can be overloaded, but no more than one of the
overloaded versions can have C linkage, because C does not allow
two extern functions to have the same name.
You can declare foo as
extern "C" int foo(...);
The ellipsis means that foo will accept any number of parameters of
any type. You can't write function foo in portable C or C++, since
there is no standard language mechanism to access the parameters.
(The <stdarg.h> mechanism requires at least one parameter of fixed
type.) In addition, you don't know what will happen if you pass an
argument of class type in a position corresponding to the ellipsis.
Maybe the C library function printf is an example of what you want
to do. A C++ declaration for it usually looks like this:
extern "C" int printf(const char*, ...);
Function printf itself would use the <stdarg.h> mechanisms to
retrieve its arguments. The fixed first parameter encodes the number
and types of its actual arguments. The function can be written
in standard C or C++, and can be called from C or C++.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/05/17 Raw View
jrwinter@mtu.edu (Jeffrey R. Winters) writes:
|> My question is best illustrated by an example.
|> Is the following legal?
|>
|>
|> extern "C"
|> {
|> //foo can take any number of 'C' parameters
|> // of any type.
|> int foo();
In order to take any number of 'C' parameters, you have to declare it
int foo( ... ) ;
Strictly speaking, this is also true in C. The form with the empty ()
does not define a function prototype (in C), and calling a variadic
function without a prototype in scope is undefined behavior.
In C++, the extern "C" only affects the calling conventions, not the
syntax of the declared function. The declaration must still be C++.
--
James Kanze home: kanze@gabi-soft.fr +33 (0)1 39 55 85 62
office: kanze@vx.cit.alcatel.fr +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
-- Conseils en informatique industrielle --
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]