#endif 之后的标记合法吗?

发布于 2024-09-14 02:57:29 字数 320 浏览 5 评论 0原文

我目前执行以下操作,编译器(MSVC2008 / 以及 2010)不会抱怨它,但我不确定这是否是一个坏主意:

#ifndef FOO_H_
#define FOO_H_

// note, FOO_H_ is not a comment:
#endif FOO_H_

我过去总是将其写为 #endif // FOO_H_ 但我发现自己今天没有这样做,并认为这很奇怪,因为显然我已经有一段时间没有使用 comment 方法了。

这是一种不好的做法,我应该返回所有标头并修复(这是一个跨平台应用程序),还是可以保持原样?

I currently do the following and the compiler (MSVC2008 / as well as 2010) doesn't complain about it but I'm not sure if it's a bad idea or not:

#ifndef FOO_H_
#define FOO_H_

// note, FOO_H_ is not a comment:
#endif FOO_H_

I used to always write it as #endif // FOO_H_ but I caught myself not doing that today and thought it was strange because apparently I've not done the comment method for a while.

Is this bad practice that I should go back through all of my headers and fix (it's a cross-platform application) or is it okay to leave it the way it is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

舂唻埖巳落 2024-09-21 02:57:29

严格来说(根据标准中的语法)同一行上的 #endif 指令后面不允许有任何标记(注释是可以的,因为它们在比预处理指令更早的翻译阶段被删除 -第 3 阶段与第 4 阶段)。

然而,MSVC 似乎允许这样做 - 我不会继续寻求修复这些问题(因为它们不会造成问题),但可能会在修改包含它们的标头时在心里记下修复它们。

当然,如果您的其他受支持的编译器发出有关它们的诊断信息,那么修复它们可能更为紧迫。

Strictly speaking (according to the grammar in the standard) no tokens are allowed following the #endif directive on the same line (comments are OK since they get removed at an earlier phase of translation than the preprocessing directives - phase 3 vs. 4).

However, MSVC seems to allow it - I wouldn't go on a quest to fix these (since they aren't causing a problem), but would probably make a mental note to fix them as you modify the headers that have them.

Of course, if your other supported compilers issue diagnostics about them it's probably more urgent to fix them.

花之痕靓丽 2024-09-21 02:57:29

AFAIK,这是不行的,它是无效的。许多编译器会忽略 #endif 之后的额外文本,并且经常会发出警告。您应该添加 // 以使其成为注释。

It is not ok, it is not valid, AFAIK. Many compilers ignore the extra text after the #endif though and often they warn about it. You should add the // to make it a comment.

你怎么这么可爱啊 2024-09-21 02:57:29

根据其他人发布的内容,我想我可以帮助您实际纠正问题。 (假设它存在于许多文件中。)

您可以使用 Visual Studio 中的查找和替换功能一次性更正所有有问题的行。只需将查找内容:设置为 "\#endif {[a-zA-Z\.\_]+}$" 并将替换为:设置为 "#endif //\1"< /code> (并确保您在查找选项下选中了 Use: [Regular Expressions]。)

并对整个解决方案执行此操作,您应该可以开始了。

(请先备份您的项目,我已经测试过它,它似乎按预期工作,但使用它需要您自担风险。)

With what everyone else posted, I figured I might help you with actually correcting the issue. (assuming it is in many files.)

You can use the Find and Replace feature in visual studio to correct all of the problematic lines at once. Just set Find What: to "\#endif {[a-zA-Z\.\_]+}$" and Replace With: to "#endif //\1" (and make sure you have Use: [Regular Expressions] checked under find options.)

And do that on the whole solution and you should be good to go.

(Please back up your project first, I have tested this and it seems to be working as intended but Use this at your own risk.)

樱花落人离去 2024-09-21 02:57:29

为什么你的编译器应该警告你。

假设你的头文件是这样的:

#ifndef X
#define X
// STUFF
// The next line does not contain an EOL marker (can happen)
#endif

现在你从源代码中包含这个

#include "plop.h"
class X
{
}

当编译器从技术上包含该文件时,扩展源代码应该如下所示

#define X
// STUFF
// The next line does not contain an EOL marker (can happen)
#endif class X
{
}

大多数现代编译器都会考虑到这种情况的发生,并在包含的文件上粘贴一个额外的 EOL 标记以防止这种情况发生发生(技术上不允许,但我想不出会导致问题的情况)。

问题是,一些较旧的编译器不提供这个额外的标记(符合更多标准),但因此您可能最终会编译上述代码(因此它们往往会警告您两件事:1)源代码中缺少 EOL文件和 2) #endif 之后的内容

Why your compiler should warn you about it.

Say your header file is like this:

#ifndef X
#define X
// STUFF
// The next line does not contain an EOL marker (can happen)
#endif

Now you include this from source

#include "plop.h"
class X
{
}

When the compiler includes the file technically the expanded source should look like this

#define X
// STUFF
// The next line does not contain an EOL marker (can happen)
#endif class X
{
}

Most modern compiler take into account his could happen and stick an extra EOL token on included files to prevent this from happening (technically not allowed but I can't think of a situation where it would cause a problem).

The problem is that some older compilers don't provide this extra token (more standards compliant) but as a result you can potentially end up compiling the above code (as a result they tend to warn you about two things 1) missing EOL in source files and 2) things after the #endif

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文