views:

276

answers:

3

When I do: less /usr/include/stdio.h (which is only a C library - nothing to do with C++)

I see __THROW after quite a few function declarations. Also, comments above a few functions say that 'This function is a possible cancellation point and therefore not marked with __THROW' What is all this for?

throw is meant to be for exception handling...but as far as I know, C doesn't provide any support for it.

Please explain.

+3  A: 

__THROW permits a function that (if used in C++) to throw exceptions (as well as indicating that it will). In C, this doesn't require consideration, hence the compile-time specific macro (which is __THROW)

If compiling via standard C, the macro typically has no effect. Its not standard, but it is widely used.

Tim Post
It's the opposite in C++… `throw()` specifies that the function *cannot* throw anything. And in standard C, it's not "not standard"; it's a valid reserved identifier and simply not for use outside the library itself. Wide usage probably just a likely coincidence.
Potatoswatter
+9  A: 

This header is likely shared between the C and C++ compiler for that vendor. Did you look what __THROW is defined as?

I suspect something akin to:

#ifdef __cplusplus
    #define __THROW throw()
#else
    #define __THROW
#endif

Or for actual specifications:

#ifdef __cplusplus
    #define __THROW(x) throw(x)
#else
    #define __THROW(x)
#endif

As you can see, in a C build, it expands to nothing. In C++, it does what you expect. This allows vendors to reuse the same file.


Just to nitpick, this isn't entirely true: "(which is only a C library - nothing to do with C++)"

The C++ standard library includes the ability to use the C standard library. The actual header is <cxxx> where xxx is the C header name. That is, to include the C header <stdlib.h> in C++, you do <cstdlib>. So it does have to do with C++. :)

This is why you see the code you do. Duplicating the header for two different languages would be a nightmare for maintenance and cleanliness.

GMan
A: 

To answer your other question concerning "This function is a possible cancellation point and therefore not marked with __THROW": This deals with multi-threading. You can "cancel" a thread, but it won't actually "cancel" until it reaches a cancellation point. Some more info: http://compute.cnr.berkeley.edu/cgi-bin/man-cgi?cancellation

Ioan