Topic: Q about a C linkage


Author: "Jeff Peil" <jpeil@bigfoot.com>
Date: 2000/05/23
Raw View
"Masao Morita" <m-morita@pdss7.trc.rwcp.or.jp> wrote in message
news:t0t66s9a5fe.fsf@pdss7.trc.rwcp.or.jp...
> I have some questions about a C linkage.
>
> The standard says some explanations about a C linkage(7.5.p4).
> But I can not understand about function declarations with a C linkage.
> For example,
>
> case A)
>   extern "C" typedef void FUNC_c();
>   FUNC_c mf;
>
> case B)
>   extern "C" void mf();
> What sort of difference between them?

Here there is no difference, both cases have "C" linkage.

> The standard says mf in case A has C++ linkage, but mf in case B has C
> linkage. Therefor there are some differences between them I suppose.
> Will you please be more specific?

Item 4 of section 7.5 says case a would have "C" linkage not "C++" linkage

> The standard says "A C language linkage is ignored for names of class
> members and the member function type of class member functions".
> For example,
>
>   extern "C" typedef void FUNC_c();
>   class C {
>     FUNC_c mf;
>   };
>   void C::mf() { }
>
> The above declarations are legal according to the standard.
> But, I think such a erroneous declaration should be ill-formed.
> Is there a reason for compatibility? Or am I missing something?

Well, the first point is member functions, clearly cannot have C linkage
(ignoring static members for the moment) as the this pointer is implicitly
passed in a non-portable fashion.  Now consider that extern "C" can wrap a
class/struct definition (extern "C" {/*...*/ class foo {/*...*/};})  making
extern "C" generate an error here would be rather unfriendly.  The next
thing to remember is that typedefs of function signatures are
member-function neutral (the same typedef'd name can be used to declare a
function in or out of a class, but the implicit this pointer is going to be
passed when its used to declare a member function.)  An important side
effect of that is
class C{/*...*/
    typedef void mf(int);
    mf func;
    mf* ptr;
};
/*in a member of C*/ptr = C::func; /*won't work -- illegal*/

I'm not sure this is ideal, but I do think alot of C++ code would be broken
by changing the typedef of a function declaration rules at all.

---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]