Forward declare entities in C standard library?

孤街浪徒 提交于 2019-12-23 18:53:15

问题


Is it legal to forward declare structs and functions provided by the C standard library?

My background is C++ in which the answer is no. The primary reason for this is that a struct or class mandated by the C++ standard library can be a template behind the scenes and may have "secret" template parameters and so cannot be properly declared with a naive non-template declaration. Even if a user does figure out exactly how to forward declare a particular entity in a particular version of a particular implementation, the implementation is not obliged to not break that declaration in future versions.

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

So is it legal to forward declare entities in the C standard library?

Another reason that entities in the C++ standard library may not be forward declared is that headers provided by the implementation need not follow the normal rules. For example, in a recent question I asked if a C++ header provided by the implementation need be an actual file and the answer was no. I don't know if any of that applies to C.

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


回答1:


Forward declarations of structs are always permissible in C. However, not very many types can be used this way. For example, you can't use a forward declaration for FILE simply because the tag name of the struct is not specified (and theoretically, it may not be a struct at all).

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

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

This used to be rather common. I think the reasoning here is that hard drives are slow, and fewer #include means faster compile times. But this isn't the 1980s any more, and we all have fast CPUs and fast hard drives, so a few #include aren't even noticed.

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

/* my code here */



回答2:


yes you can this is perfectly valid. this can be done with the standard library too.

double atof(const char *);

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

#include <stdio.h>

Similiar things can be done with structs, variables etc.

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

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

this is specified in the c standard, Section 7.1.4 paragraph 2 of n1570

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



来源:https://stackoverflow.com/questions/27720258/forward-declare-entities-in-c-standard-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!