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

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

问题描述

我正在阅读一个文件中同一标题的多个包含,并发现了一个有趣的语句(链接):

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).

我知道这些技巧在现实世界的项目中可能是不可取的和混乱的针对多个内容,例如包含警卫 #pragma once )。
但是,这些技巧是什么?我想出了几个想法,但想看到一些实际的例子(理想情况下,安全和尝试)。

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中的伪模板,其中模板参数替换为预处理器定义。


  • 每个块的结构/类结构都是可以实现的,它可以在没有包含的情况下完成,但是函数可能太大或太多,所以创建一个单独的文件是有意义的。 (片段的连接)。
  • 查找表和其他编译时数据结构(再次,借助于预处理器定义)可以帮助模拟C中的继承和防止代码重复。 。

  • Pseudo-templates in C, where template parameters are substituted with preprocessor definitions. It can be done without inclusions, but functions may be too big or too numerous, so making a separate file would make sense.
  • Block-by-block struct/class construction (concatenation of pieces). It may help emulate inheritance in C and prevent code duplication when defining structs with common members.
  • Look-up tables and other compile-time data structures (again, with the aid of preprocessor definitions).

推荐答案

#includefile表示获取头文件并将其所有内容而不是 #include 行。

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

em>通常使用标题进行类型定义和向源文件添加转发声明。在文件中定义相同的类型两次(循环包含将总是导致它)给出编译错误,因此我们使用 #ifndef #pragma once 。 (或两者)

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)

但我们也可以放入重复的代码和宏,并包含它几次,即使在同一个文件。在这种情况下,我们不会使用 #ifndef #pragma once

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 )会导致一系列警告,并且它会打扰你,你确定你的代码是好的,但你不想禁用所有项目或文件上的所有警告,你只是想要在调用特定函数时禁用它。

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__

这将使你的代码看起来很脏,尤其是如果你多次调用该函数。

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

一个可能的解决方案,(我不建议它是最好的一个...)是使2个标题,在一个抑制警告,在另一个取消抑制。

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.

在这种情况下,代码可能如下所示:

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天全站免登陆