Topic: Extern "C" functions.


Author: jbdp@cix.compulink.co.uk ("Julian Pardoe")
Date: 1997/12/17
Raw View
Could someone please tell me what the state of play is with extern "C"
functions and pointers thereto.

1) Can I write a C-callable function in C++?  (I seem to recall that the
function-pointer argument to 'qsort' was an exceptional case in one draft
of the standard.)

2) Can I have a variable of type "pointer to C function..."?

3) Can I have a typedef type "pointer to C function..."?

4) Can I have a static function of type "C function..."?

Personally I'd like to be able to write
    typedef "C" int (*CompareFun) (const char *, const char *);
and
    static "C"  int (*CompareFun) (const void *, const void *);
rather than the apparently self-contradictory
    extern "C"
        {
        static int (*CompareFun) (const void *, const void *);
        };
that I have to with some compilers.

-- jP --


---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Fergus Henderson,L2.19,9170,3481669 <fjh@mundook.cs.mu.OZ.AU>
Date: 1997/12/22
Raw View
In article <ELCs9D.234@cix.compulink.co.uk>,
Julian Pardoe <jbdp@cix.compulink.co.uk> wrote:
>Could someone please tell me what the state of play is with extern "C"
>functions and pointers thereto.
>
>1) Can I write a C-callable function in C++?  (I seem to recall that the
>function-pointer argument to 'qsort' was an exceptional case in one draft
>of the standard.)

Yes.  For example:

 extern "C" void foo() {}

In the Nov 97 draft, qsort() and bsearch() are overloaded: they can
be passed pointers to functions with either C or C++ linkage
(calling convention).

>2) Can I have a variable of type "pointer to C function..."?

Yes.  For example:

 extern "C" void (*ptr)();

Note that `ptr' itself has C linkage, as well as pointing to a function
which has C linkage (calling convention).

>3) Can I have a typedef type "pointer to C function..."?

Yes.

 extern "C" typedef void (*ptr_typedef)(void);

>4) Can I have a static function of type "C function..."?

Yes.

 extern "C" {
  static void foo(void);
 }

>Personally I'd like to be able to write
>    typedef "C" int (*CompareFun) (const char *, const char *);
>and
>    static "C"  int (*CompareFun) (const void *, const void *);
>rather than the apparently self-contradictory
>    extern "C"
>        {
>        static int (*CompareFun) (const void *, const void *);
>        };
>that I have to with some compilers.

The apparently self-contradictory syntax is the one mandated by
the standard.

C has a long history of conflating unrelated notions
(e.g. `char' and `small integer') and attempting to avoid
"unnecessary" complexity (e.g. no bool type).
C++ continued in this fashion by conflating "linkage" and
"calling convention", and by resisting the natural step
of making calling convention part of the type.
(This resistence -- like the resistence to `bool' -- was
in fact unsuccessful in the long run.)

Did this approach lead to simplicity?
Well, you can make your own minds up on that one ;-)

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1997/12/24
Raw View
Fergus Henderson ; L2.19 ; 9170 <9170>; 3481669 wrote in message ...

>In the Nov 97 draft, qsort() and bsearch() are overloaded: they can
>be passed pointers to functions with either C or C++ linkage
>(calling convention).

>In article <ELCs9D.234@cix.compulink.co.uk>,
>Julian Pardoe <jbdp@cix.compulink.co.uk> wrote:

>>2) Can I have a variable of type "pointer to C function..."?
>
>Yes.  For example:
>
> extern "C" void (*ptr)();


Could I please hear some chapter & verse on this answer? I was under the
impression that, while functions could have C linkage, pointers-to-function
always pointed to C++ functions. By that rule, the example in your answer
would declare a pointer-to-C++-function, but the pointer variable "ptr"
would have C linkage externally.

I included the bit about qsort() and bsearch() also, because it only makes
sense if linkage has become part of the variable's type. When was this
change made?
---
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds
---
[ 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                             ]