Does forward declare objects in the C standard library?

Is it legal to override the declaration of structures and functions provided by the C standard library?

My background is C ++ where the answer is no. The main reason for this is that a structure or class provided by the C ++ standard library can be a template behind the scenes and can have "secret" template parameters and therefore cannot be declared with a naive, non-template declaration. Even if the user specifies exactly how to redirect a declaration for a specific object to a specific version of a specific implementation, an implementation need not violate that declaration in future versions.

I don't have a copy of any C standard, but obviously there are no templates in C.

So, is it legal to forward declarations to the C standard library?

Another reason why entities in the C ++ Standard Library cannot be declared with branching is that implementation-supplied headers should not follow normal rules. For example, in a recent question, I asked if the C ++ header provided by the implementation should be an actual file and the answer shouldn't. I don't know if this applies to C.

The C standard library is used by both C and C ++, but for this question, I'm only asking about C.

+3


source to share


2 answers


Variable structure declarations are always valid in C. However, not many types can be used in this way. For example, you cannot use a forward declaration for FILE

simply because the tag name of the structure is not specified (and in theory it might not be a structure at all).

Section 7.1.4 paragraph 2 n1570 gives you permission to do the same with functions:

Provided that a library function can be declared without referencing any type defined in the header, it is also allowed to declare the function and use it without including its associated header.



This was quite common. I think the reasoning here is that hard drives are slow and less #include

means faster compilation times. But this is no longer the 80s, and we all have fast processors and fast hard drives, so a few are #include

not even noticed.

void *malloc(size_t);
void abort(void);

/* my code here */

      

+5


source


yes you can do it perfectly right. this can be done with the standard library as well.

double atof(const char *);

int main() {
    double t = atof("13.37");
    return 0;
}

#include <stdio.h>

      

Compatible things can be done with structures, variables, etc.

I would recommend that you read the wiki page which provides some c examples:



http://en.wikipedia.org/wiki/Forward_declaration

this is specified in standard c, section 7.1.4 paragraph 2 n1570

Provided that a library function can be declared without referencing any type defined in the header, it is also allowed to declare the function and use it without including its associated header.

0


source







All Articles