当它是有用的相同的标题多次包含在一个文件? [英] When is it useful to include the same header multiple times in one file?

查看:127
本文介绍了当它是有用的相同的标题多次包含在一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到一个文件相同的标题的多个包裹,并发现了一个有趣的说法(链接):

I was reading about multiple inclusions of the same header in one file, and found an interesting statement (link):

有一对夫妻带着头文件的技巧是你故意
  包括它多次(这样做实际上是提供一个有用的
  功能)。

There are a couple of tricks with header files were you deliberately include it multiple times (this does actually provide a useful feature).

据我所知,这些技巧可能是在现实世界项目(尤其是因为人拿precautions对多种夹杂物,像的包括警卫 的#pragma一次 ) 。
但尽管如此,那些是什么招数?我想出了一些想法,但希望看到一些实际的例子(理想情况下,安全试过)。

I understand that those tricks are probably undesired and confusing in real-world projects (especially since people take precautions against multiple inclusions, like include guards and #pragma once). But still, what are those tricks? I came up with a few ideas, but would like to see some actual examples (ideally, safe and tried).

我的想法:


    在C,其中的模板参数代以preprocessor定义
  • 伪模板。
    它可以在不夹杂做到的,但功能可能会受到太大或太多,所以让一个单独的文件是有意义的。

  • 块逐块结构/类结构(件串联)。它可以帮助定义与普通成员结构时用C仿效继承和prevent code重复。

  • 查找表和其它编译时数据结构(再次,用preprocessor定义助剂)。

推荐答案

的#include文件表示取头文件,并把它的所有内容,而不是的的#include 行。

#include "file" means take the header file and put all of its content instead of the #include line.

我们一般的类型定义和添加前置声明到源文件中使用标头。在一个文件中定义两次相同的类型(圆形包括总会引起它)给出编译错误,因此我们使用的#ifndef 的#pragma一次。 (或两者)

We usually used headers for type definitions and for adding a forward declarations to a source files. defining same type twice in a file (a circular include will always cause it) gives compilation error, therefore we use #ifndef or #pragma once. (or both)

但我们也可以把重复code和宏,包括了好几次,甚至在同一个文件。在如情况下,我们不会使用的#ifndef 也不的#pragma一次。如果你这样做,所以你必须要格外小心,并做到这一点只有当你知道自己在做什么。

But we also can to put a repeating code and macros and include it several times, even in the same file. in such as case, we won't use #ifndef nor #pragma once. If you do so you must be extra careful, and do it only if you know what you are doing.

例如:如果在某些操作系统调用特定系统功能(甚至像AC宏: offsetof )导致一串警告,并且它困扰着你,和你确定你的code是不错的,但你不希望禁用您在所有项目或文件得到了所有警告,你只是想,当你调用来禁用它特定的功能。

For example: If in some OS calling a specific system function (or even a c macro like: offsetof) cause a bunch of warnings, and it is bothering you, and you sure your code is good, but you don't want to disable all the warnings you've got on all the project or the file, you just want to disable it when you call the specific function.

//suppress the warnings: 
#if defined(__GNUC__)
  #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wreorder"
    #pragma GCC diagnostic ignored "-Wunused-function"
    #pragma GCC diagnostic ignored "-Wunused-variable"
    #pragma GCC diagnostic ignored "-Wsign-compare"
    #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
    #pragma GCC diagnostic ignored "-Wsequence-point"
  #endif
#endif // __GNUC__

//here you call the function...
func(x,y,z);

//unsupress: bring back the warnings to normal state
#if defined(__GNUC__)
  #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
    #pragma GCC diagnostic pop
  #endif
#endif // __GNUC__

这会使你的code看起来非常脏,尤其是当你调用该函数几次。

This will make your code to look very dirty, especially if you call the function several times.

一个可能的解决方案,(我并不是说这是最好的一个......)是使2头,在一个共进晚餐preSS的警告,并在其他取消SUP pression

One possible solution, (I'm not suggesting it is the best one...) is to make 2 headers, in one to suppress the warnings and in the other to cancel the suppression.

在这种情况下,你的code可能是这样的:

In that case your code may look like this:

#include "suppress.h"
func(x,y,z);
#include "unsuppress.h"

//.... more code come here 
//now when call it again:
#include "suppress.h"
func(x,y,z);
#include "unsuppress.h"

这篇关于当它是有用的相同的标题多次包含在一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆