C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Static member function with C linkage

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alan McKenney
Guest





PostPosted: Wed Sep 08, 2004 8:22 am    Post subject: Static member function with C linkage Reply with quote



How do I declare (and define) a static member function
in a class to have C linkage?

The obvious syntax:

class T
{
...

extern "C" {
static void f( T *arg );
}
};

doesn't compile on my system (Solaris w/ Solaris compiler.)

Obviously, I could declare it as a global function, but
I want to make it a static member for the usual reasons
one prefers a function to be a static member rather than
global.

-- Alan McKenney

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gerhard Menzl
Guest





PostPosted: Wed Sep 08, 2004 7:15 pm    Post subject: Re: Static member function with C linkage Reply with quote



Alan McKenney wrote:

Quote:
How do I declare (and define) a static member function
in a class to have C linkage?

You can't. "A C language linkage is ignored for the names of class
members and the member function type of class member functions." (7.5/4)

Quote:
The obvious syntax:

class T
{
...

extern "C" {
static void f( T *arg );
}
};

doesn't compile on my system (Solaris w/ Solaris compiler.)

Obviously, I could declare it as a global function, but
I want to make it a static member for the usual reasons
one prefers a function to be a static member rather than
global.

You can solve the problem using that time-honoured panacea called extra
level of indirection:

extern "C" void f (T* arg);

void f (T* arg)
{
T::f (arg);
};

Note that if you really want to use this function from C, you won't be
happy with the class type T either.

--
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Wed Sep 08, 2004 7:20 pm    Post subject: Re: Static member function with C linkage Reply with quote



In article <16a885f9.0409071647.be912e5 (AT) posting (DOT) google.com>, Alan
McKenney <alan_mckenney1 (AT) yahoo (DOT) com> writes
Quote:
How do I declare (and define) a static member function
in a class to have C linkage?

Well first you will have to explain how to generate a link name for such
a function.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Wed Sep 08, 2004 7:22 pm    Post subject: Re: Static member function with C linkage Reply with quote

In article <16a885f9.0409071647.be912e5 (AT) posting (DOT) google.com>, Alan
McKenney <alan_mckenney1 (AT) yahoo (DOT) com> wrote:

Quote:
How do I declare (and define) a static member function
in a class to have C linkage?

The obvious syntax:

class T
{
...

extern "C" {
static void f( T *arg );
}
};

doesn't compile on my system (Solaris w/ Solaris compiler.)

Obviously, I could declare it as a global function, but
I want to make it a static member for the usual reasons
one prefers a function to be a static member rather than
global.

In short it should not compile but you can have the best of both

worlds by forwanding it to a global function such as

extern "C" void CAPI_T_f(T *arg);

class T
{
// ...
static void f(T *arg) {return CAPI_T_f(arg);}
};

and never use CAPI_T_f(T*) directly.

extern "C" means "remove name mangling" and static member functions
have "name mangling". So the best you can do is mangle it is to
mangle it yourself and wrap it in a static member function like above.

placing the code in the class defintion makes it inlinable and most
compilers will traslate this to a direct call to the external C
function.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alan McKenney
Guest





PostPosted: Mon Sep 13, 2004 9:04 pm    Post subject: Re: Static member function with C linkage Reply with quote

Carl Barron <cbarron413 (AT) adelphia (DOT) net> wrote

Quote:
In article <16a885f9.0409071647.be912e5 (AT) posting (DOT) google.com>, Alan
McKenney <alan_mckenney1 (AT) yahoo (DOT) com> wrote:

How do I declare (and define) a static member function
in a class to have C linkage?

<snip>

Quote:
extern "C" means "remove name mangling" and static member functions
have "name mangling". So the best you can do is mangle it is to
mangle it yourself and wrap it in a static member function ....

<snip>

What if I don't need to remove name mangling, but I want to insure
that if I pass a pointer to the function to a "C" function, it can
call it?

Note that extern "C" means more than "remove name mangling";
for example, if C passes arguments on the stack, but C++ uses
a separate struct for args (unlikely, but possible), it would indicate
to expect arguments on the stack, not in a struct. Or if C expects
return values in a register, but C++ returns them on the stack....

For example:

void *my_thread_func(void *arg) { .... }

void start_thread()
{
pthread_create( &id, &attr, &my_thread_func, &dummy );
}

where "pthread_create()" is from a library written in C.

In this case, I don't care about name mangling, since
the C code doesn't need to know the name (it's getting a pointer.)
But it will be called by C code, so if argument- and return value-
passing conventions are different, the C++ compiler needs to generate
different code.

The Solaris compiler allows this (with no extern "C"), presumably
because the C++ and C compilers use the same arg. and ret. val.
passing conventions, but (correctly) warns that this is not
standard-conforming.

Moreover, in another application, the function to be called by
the C function is generated by a template, which requires
name-mangling to avoid symbol collisions.

Alan McKenney
[email]alan_mckenney1 (AT) yahoo (DOT) com[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Tue Sep 14, 2004 12:06 pm    Post subject: Re: Static member function with C linkage Reply with quote

In article <16a885f9.0409131116.37e7e3c3 (AT) posting (DOT) google.com>, Alan
McKenney <alan_mckenney1 (AT) yahoo (DOT) com> wrote:


Quote:
In this case, I don't care about name mangling, since

the C code doesn't need to know the name (it's getting a pointer.)
But it will be called by C code, so if argument- and return value-
passing conventions are different, the C++ compiler needs to generate
different code.

The Solaris compiler allows this (with no extern "C"), presumably
because the C++ and C compilers use the same arg. and ret. val.
passing conventions, but (correctly) warns that this is not
standard-conforming.

Moreover, in another application, the function to be called by
the C function is generated by a template, which requires
name-mangling to avoid symbol collisions.

Alan McKenney
[email]alan_mckenney1 (AT) yahoo (DOT) com[/email]

Ok now the signature of a static member function and a global

function is the same: [return type and argument types ]
struct foo
{
static int do_it(int,char *);
};

extern "C" void fudge(int (*)(int,char *),int,char *);
fudge(foo::do_it,10,"This is junk"):

is legal and has no name_mangling, is this what you want to do, just
not compile foo::do_It as so that is directly callable in C. just that
it can be called indirectly from C via a fuiction pointer.

Yes I know extern "C" means more than skip the name mangling but that
is guaranteed, and other things are implimentation defined, such as
argument passage strategies.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alan McKenney
Guest





PostPosted: Wed Sep 15, 2004 11:43 pm    Post subject: Re: Static member function with C linkage Reply with quote

Carl Barron <cbarron413 (AT) adelphia (DOT) net> wrote

Quote:
In article <16a885f9.0409131116.37e7e3c3 (AT) posting (DOT) google.com>, Alan
McKenney <alan_mckenney1 (AT) yahoo (DOT) com> wrote:


In this case, I don't care about name mangling, since

the C code doesn't need to know the name (it's getting a pointer.)
But it will be called by C code, so if argument- and return value-
passing conventions are different, the C++ compiler needs to generate
different code.

The Solaris compiler allows this (with no extern "C"), presumably
because the C++ and C compilers use the same arg. and ret. val.
passing conventions, but (correctly) warns that this is not
standard-conforming.

Moreover, in another application, the function to be called by
the C function is generated by a template, which requires
name-mangling to avoid symbol collisions.

Alan McKenney
[email]alan_mckenney1 (AT) yahoo (DOT) com[/email]

Ok now the signature of a static member function and a global
function is the same: [return type and argument types ]
struct foo
{
static int do_it(int,char *);
};

extern "C" void fudge(int (*)(int,char *),int,char *);
fudge(foo::do_it,10,"This is junk"):

is legal and has no name_mangling, is this what you want to do, just
not compile foo::do_It as so that is directly callable in C. just that
it can be called indirectly from C via a fuiction pointer.

Yes I know extern "C" means more than skip the name mangling but that
is guaranteed, and other things are implimentation defined, such as
argument passage strategies.

The $64 question: is this standard-conforming? You say it is "legal",
which in this newsgroup usually means "standard-conforming", but
my compiler and my intuition say it is not.

extern "C" was added to C++ to make sure that the correct way
to call C from C++ and vice-versa would *not* be implementation-
defined. If the argument-passing strategies differ between C and
C++, it is the responsibility of the implementation to make
the C++ function declared as extern "C" work correctly anyway
when calling/being called from C.

Your solution, which is what is used in some of the code I am working
with, does work on my current platform (Solaris), but the compiler
warns that it is not standard-conforming. My previous post suggested
a situation in which it would break.

I am looking for a way to do this (pass a C++ function pointer to a C
function) in a way that works on *any* standard-conforming
implementation, and still allows me to pass pointers to (static) member
or (static) template functions.

If there isn't one, fine, but I'd like to *know* that, and not just
have to guess.

-- Alan McKenney
[email]alan_mckenney1 (AT) yahoo (DOT) com[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Fri Sep 17, 2004 2:06 am    Post subject: Re: Static member function with C linkage Reply with quote

In article <16a885f9.0409150717.5181a2b3 (AT) posting (DOT) google.com>, Alan
McKenney <alan_mckenney1 (AT) yahoo (DOT) com> writes
Quote:
extern "C" was added to C++ to make sure that the correct way
to call C from C++ and vice-versa would *not* be implementation-
defined. If the argument-passing strategies differ between C and
C++, it is the responsibility of the implementation to make
the C++ function declared as extern "C" work correctly anyway
when calling/being called from C.


Did you think that through? C does not define a binary interface. There
is no requirement that there should be a C compiler for all platforms
with a C++ compiler. So exactly how do you propose that a C++
implementation ensure that extern "C" work universally for all C
compilers that might be used to compile C source code that might call or
be called by some C++?

A C++ implementation has to define what it provides in a context of
extern "C" so that users can know which, if any, C compilers will
generate link compatible code. To ask for anything more requires an
omniscient C++ implementation.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michiel Salters
Guest





PostPosted: Fri Sep 17, 2004 2:10 am    Post subject: Re: Static member function with C linkage Reply with quote

[email]alan_mckenney1 (AT) yahoo (DOT) com[/email] (Alan McKenney) wrote in message news:<16a885f9.0409131116.37e7e3c3 (AT) posting (DOT) google.com>...
Quote:
Carl Barron <cbarron413 (AT) adelphia (DOT) net> wrote

In article <16a885f9.0409071647.be912e5 (AT) posting (DOT) google.com>, Alan
McKenney <alan_mckenney1 (AT) yahoo (DOT) com> wrote:

extern "C" means "remove name mangling" and static member functions
have "name mangling". So the best you can do is mangle it is to
mangle it yourself and wrap it in a static member function ....

snip

What if I don't need to remove name mangling, but I want to insure
that if I pass a pointer to the function to a "C" function, it can
call it?

Use extern "C". It's the only thing the standard gives you.
The standard doesn't care how a certain C and C++ implementation
differ. E.g. an implementation that implements name mangling
for C as well wouldn't care about that part. An implementation
which determines the value of pointers to functions at load
time, by name, would have serious problems if the name and
linkage didn't match.

Regards,
Michiel Salters

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.