mynk mynk - 2 months ago 6
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
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
What is all this for?

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

Please explain.


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()
    #define __THROW

Or for actual specifications:

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

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.