简体   繁体   English

有条件的上课汇编

[英]Conditional compilation of class

Normally, for classes I don't intend to include in production code I have conditional operators such as the usual: 通常,对于我不打算在生产代码中包括的类,我有条件运算符,例如通常:

#ifdef DEBUG_VERSION

This could also be around certain chunks of code that performs additional steps in development mode. 这也可能与在开发模式下执行附加步骤的某些代码块有关。

I've just thought (after many years or using the above): What happens if a typo is introduced in the above? 我只是想(经过多年或使用以上方法):如果在上面引入了错字怎么办? It could have great consequences. 这可能会产生很大的后果。 Pieces of code included (or not included) when the opposite was intended. 意图相反时包含(或不包括)代码段。

So I'm now wondering about alternatives, and thought about creating 2 macro's: 因此,我现在想知道替代方法,并考虑创建2个宏:

INCLUDE_IN_DEBUG_BUILD
END_INCLUDE_IN_DEBUG_BUILD

If a typo is ever created in these, an error message is created at compile time, forcing the user to correct it. 如果曾经在其中创建错别字,则会在编译时创建一条错误消息,迫使用户对其进行更正。 The first would evaluate to "if (1){" in the debug build and "if (0){" in the production build, so any compiler worth using should optimise those lines out, and even if they don't, at least the code inside will never be called. 第一个在调试版本中将评估为“ if(1){”,在生产版本中将评估为“ if(0){”,因此,任何值得使用的编译器都应优化这些行,即使没有,也应至少对其进行优化。里面的代码将永远不会被调用。

Now I'm wondering: Is there something I'm missing here? 现在我想知道:我这里缺少什么吗? Why does no-one else use something like this? 为什么没有人使用这样的东西?

Update: I replaced the header-based approach with a build-system based approach. 更新:我将基于标头的方法替换为基于构建系统的方法。

You want to be able to disable not just part of the code inside a function, but maybe also in other areas like inside a class or namespace: 您希望不仅能够禁用函数内的部分代码,而且还可能禁用类或名称空间内的其他区域:

struct my_struct {
#ifdef DEBUG_VERSION
    std::string trace_prefix;
#endif
};

So the real question seems to be: How to prevent typos in your #ifdef s? 因此,真正的问题似乎是:如何防止#ifdef出现拼写错误? Here's something which does not limit you and which should work well. 这不是限制您的事情,它应该可以正常工作。

Modify your build system to either define DEBUG_VERSION or RELEASE_VERSION . 修改您的构建系统以定义DEBUG_VERSIONRELEASE_VERSION It should be easy to ensure this. 确保这一点应该很容易。 Define those to nothing, eg -DDEBUG_VERSION or -DRELEASE_VERSION for GCC/Clang. 将它们定义为-DDEBUG_VERSION ,例如-DDEBUG_VERSION-DRELEASE_VERSION用于GCC / Clang。

With this, you can protect your code like this: 这样,您可以像这样保护代码:

#ifdef DEBUG_VERSION
DEBUG_VERSION
// ...
#endif

or 要么

#ifndef DEBUG_VERSION
DEBUG_VERSION
// ...
#else
RELEASE_VERSION
// ...
#endif

And voila, in the second example above, I already added a small typo: #ifndef instead of #ifdef - but the compiler would complain now as DEBUG_VERSION and RELEASE_VERSION are not defined (as in "defined away" by the header) in the corresponding branches. 瞧,在上面的第二个示例中,我已经添加了一个小的错字: #ifndef而不是#ifdef DEBUG_VERSION但是编译器现在会抱怨,因为未在相应的文件中定义DEBUG_VERSIONRELEASE_VERSION (如标头中所定义)分支机构。

To make it as safe as possible, you should always have both branches with the two defines, so the first example I gave should be improved to: 为了使它尽可能安全,应该始终同时具有两个定义的两个分支,因此,我给出的第一个示例应改进为:

#ifdef DEBUG_VERSION
DEBUG_VERSION
// ...
#else
RELEASE_VERSION
#endif

even if the release branch contains no other code/statements. 即使release分支不包含其他代码/语句。 That way you can catch most errors and I think it is quite descriptive. 这样,您可以捕获大多数错误,并且我认为它具有描述性。 Since the DEBUG_VERSION is replaced with nothing only in the debug branch, all typos will lead to a compile-time error. 由于DEBUG_VERSION仅在debug分支中被替换为DEBUG_VERSION ,因此所有错字都将导致编译时错误。 The same for RELEASE_VERSION . 对于RELEASE_VERSION

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM