简体   繁体   English

在C / C ++预处理器中,单个磅/井号(#)在其自己的行上的目的是什么?

[英]What is the purpose of a single pound/hash sign (#) on its own line in the C/C++ preprocessor?

I have been looking at the Boost libraries source code, and I have noticed that often there are single pound signs without any preprocessor directives attached to them. 我一直在查看Boost库的源代码,我注意到通常只有单个符号没有附加任何预处理器指令。 I read through the GCC preprocessor manual and specification guide and can't find anything about it. 我阅读了GCC预处理器手册和规范指南,但没有找到任何相关信息。

(1) #ifndef BOOST_CONFIG_HPP
(2) #  include <boost/config.hpp>
(3) #endif
(4) #
(5) #if defined(BOOST_HAS_PRAGMA_ONCE)
(6) #  pragma once
(7) #endif

On line 4, there is nothing after the pound sign. 在第4行,英镑符号后没有任何内容。 What effect does this have? 这有什么影响? Is it defined in the C preprocessor (CPP) specification? 它是在C预处理器(CPP)规范中定义的吗?

As Boost is a cross-platform library, I would assume that any CPP should parse it correctly. 由于Boost是一个跨平台的库,我认为任何CPP都应该正确解析它。 What would the effect/side-effects be of having random pound/hash signs throughout the code? 在整个代码中使用随机磅/哈希符号会产生什么影响/副作用?

A # on its own on a line has no effect at all. 一条线路上的#根本没有任何影响。 I assume it's being used for aesthetic value. 我认为它被用于审美价值。

The C standard says: C标准说:

6.10.7 Null directive 6.10.7空指令

Semantics 语义

A preprocessing directive of the form 表单的预处理指令

# new-line

has no effect. 没有效果。

The C++ standard says the same thing: C ++标准说同样的事情:

16.7 Null directive [cpp.null] 16.7空指令[cpp.null]

A preprocessing directive of the form 表单的预处理指令

# new-line

has no effect. 没有效果。

It makes the source code look pretty, that's all. 它使源代码看起来很漂亮,就是这样。

Highlights the fact that the whole block is a preprocessor section. 突出显示整个块是预处理器部分的事实。

And indeed, both the C and C++ preprocessors must ignore # on a line. 事实上,C和C ++预处理器都必须忽略一行中的#

Always check an authoritative source instead of relying on other resources. 始终检查权威来源,而不是依赖其他资源。 C is standardised as ISO 9899::2011, C++ also has an ISO standard. C标准化为ISO 9899 :: 2011,C ++也有ISO标准。 Both are well accepted and the final drafts available by a short search. 两者都被广泛接受,并通过简短搜索获得最终草稿。 The C standard states in 6.10.7 (C++ has much the same text): C标准在6.10.7声明 (C ++有相同的文本):

A preprocessing directive of the form 表单的预处理指令

 # new-line 

has no effect. 没有效果。

This is a null directive , as much as an ; 这是一个空指令 ,就像一个; without a preceeding expression in the core-language is a null statement . 在核心语言中没有前面的表达式是一个空语句

For the preprocessor it is just for formatting/readability to highlight that the lines belong semantically together. 对于预处理器,它只是为了格式化/可读性突出显示这些行在语义上属于一起。 (the semicolon OTOH is semantically relevant). (分号OTOH在语义上相关)。

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

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