Topic: extern C static
Author: skaller@users.sourceforge.net (skaller)
Date: Tue, 15 Aug 2006 16:04:28 GMT Raw View
On Mon, 14 Aug 2006 12:43:59 -0600, kuyper wrote:
> skaller wrote:
>> Is the following well formed?
>>
>> extern "C" { static void f() {} }
> Do you have you reason to think it might be ill-formed?
Yes: the wording of the ISO C++ Standard on this matter
is ambiguous in my mind. It says the program is ill formed
if the extern "C" consists of exactly one declaration ..
and that is the case in the code above.
I suspect that the intent is to allow the above, and the
distinguishing feature is the presence of the { }.
I could fix the above code by:
extern "C" {
static void f() {}
typedef int stupid_dummy_type;
}
so that now the extern "C" contains TWO declarations.
>I suspect that
> if you're having problems, their cause is elsewhere, though the error
> message might refer to this line of code.
I'm not having problems. All the compilers I tried accept:
extern "C" static void f() {};
even though it is not well formed -- that example is
actually given in the Standard as an ill formed case.
I just wish to clarify that in fact the above example
is NOT ill formed -- as matter of Standard pedantry :)
--
John Skaller <skaller at users dot sf dot net>
Try Felix, the successor to C++ http://felix.sf.net
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: skaller@users.sourceforge.net (skaller)
Date: Tue, 15 Aug 2006 16:12:20 GMT Raw View
On Tue, 15 Aug 2006 08:44:27 -0600, Greg Herlihy wrote:
> skaller wrote:
>> Is the following well formed?
>>
>> extern "C" { static void f() {} }
> Why not place the callbacks in an anonymous namespace?
Because I am not certain the Standard guarantees such names
are not also visible in one, or indeed ALL other namespaces,
including the global namespace. Last I remember, no such
assurance is made, for the simple reason it could break
implementations which provide C Standard library functions
in BOTH the global namespace AND in namespace std -- but
I could be wrong on that:
#include <math.h>
#include <cmath>
assert(&::sin == &std::sin)
These issues were discussed a lot on the committee whilst I
was a member, however I don't know the current status,
however common sense dictates that since extern "C" linker
names can't have any namespace qualifiers .. there's only
really one namespace of extern "C" entities .. so putting
a declaration in an anonymous .. or any other .. namespace
won't make any difference: the global namespace gets polluted
anyhow (even if only at link time, not lookup time).
It was actually me that suggested that anonymous namespaces
were normal namespaces with magical names, so that stuff
in them had external linkage .. but didn't pollute the
containing namespace or permit qualified reference from
outside, but extern "C" stuff is special.
--
John Skaller <skaller at users dot sf dot net>
Try Felix, the successor to C++ http://felix.sf.net
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: kuyper@wizard.net
Date: Wed, 16 Aug 2006 11:27:04 CST Raw View
skaller wrote:
> On Mon, 14 Aug 2006 12:43:59 -0600, kuyper wrote:
>
> > skaller wrote:
> >> Is the following well formed?
> >>
> >> extern "C" { static void f() {} }
>
>
> > Do you have you reason to think it might be ill-formed?
>
> Yes: the wording of the ISO C++ Standard on this matter
> is ambiguous in my mind. It says the program is ill formed
> if the extern "C" consists of exactly one declaration ..
> and that is the case in the code above.
Citation, please? I couldn't find any such restriction, though I may
have been looking in the wrong place.
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: skaller@users.sourceforge.net (skaller)
Date: Mon, 14 Aug 2006 16:19:36 GMT Raw View
Is the following well formed?
extern "C" { static void f() {} }
??
If not, can someone please propose it, it is required
for callbacks which one wishes to have internal linkage.
[Whether or not the C++ Standard mandates the use of C
calling convention here, most C++ compilers do precisely
this and must, or C++ would be entirely useless because
it wouldn't be able to call C functions .. one of the
main reasons for using C++ being C compatibility :]
--
John Skaller <skaller at users dot sf dot net>
Try Felix, the successor to C++ http://felix.sf.net
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: kuyper@wizard.net
Date: Mon, 14 Aug 2006 12:43:59 CST Raw View
skaller wrote:
> Is the following well formed?
>
> extern "C" { static void f() {} }
I believe so. However, just in case I was missing some tricky detail, I
tried it with the Coumeau online compiler. It only gives a warning
about f being unused, which is a very reasonable warning when a static
function is declared. I initialized a non-static variable with a
pointer to that function, and the warning disappeared. I presume that
if you need it for a call back, your code actually uses it.
Do you have you reason to think it might be ill-formed? I suspect that
if you're having problems, their cause is elsewhere, though the error
message might refer to this line of code.
> If not, can someone please propose it, it is required
> for callbacks which one wishes to have internal linkage.
>
> [Whether or not the C++ Standard mandates the use of C
> calling convention here, most C++ compilers do precisely
> this and must, or C++ would be entirely useless because
> it wouldn't be able to call C functions .. one of the
> main reasons for using C++ being C compatibility :]
Most of the details are implementation-specific, but the C++ standard
requires that f()'s type have "C" language linkage, which means
precisely that it's supposed to use the C calling conventions. Whether
there's an actual C compiler that actually uses those calling
conventions is outside the scope of the C++ standard. However, you're
guaranteed that the function can be called from another translation
unit in the same C++ program using an extern "C" function type, and
have it work correctly.
Note: you need to pay careful attention to data types and type
conversions here. If the C function that needs a callback pointer
documents a function type for that pointer of "void (*)(void)", then
C++ code that passes that callback needs to use a pointer type of
'extern "C" void (*)(void)'. Even inside an extern "C" block, the code
is interpreted according as C++ code, so the second 'void' is optional.
This is not pointer type not necessarily the same as, or even
compatible with, a data type of 'exterm "C++" void(*)()'.
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: "Greg Herlihy" <greghe@pacbell.net>
Date: Tue, 15 Aug 2006 08:44:27 CST Raw View
skaller wrote:
> Is the following well formed?
>
> extern "C" { static void f() {} }
Yes - it means the name of the function f() has internal linkage (not C
language linkage) and that f()'s type has C language linkage (see
7.5/4 in the latest draft).
Note that this declaration should not be confused with the following,
ill-formed example that attempts to declare f() both extern and static:
extern "C" void f();
static void f() {} // Error - f previously declared extern "C"
> If not, can someone please propose it, it is required
> for callbacks which one wishes to have internal linkage.
Why not place the callbacks in an anonymous namespace?
Greg
---
[ 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.comeaucomputing.com/csc/faq.html ]