mynk mynk - 3 months ago 8
C Question

Why do I see THROW in a C library?

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.

Answer

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.