简体   繁体   English

在链接规范内(外部“ C”,外部“ C ++”)在C ++代码中包括标准C头的指定行为是什么?

[英]What is the specified behavior of including a standard C header, in C++ code, inside a linkage-specification (extern “C”, extern “C++”)?

Typically one includes standard library headers in C++ in the global namespace, outside of any extern s, like so: 通常,在任何extern之外,在全局命名空间中都包括C ++中的标准库头,如下所示:

#include <stdint.h>

int main() { }

But what's specified to happen if you include a standard library header inside one? 但是,如果您在其中包含一个标准库头,指定发生什么呢? For example: 例如:

extern "C" {
#include <stdint.h>
}
int main() { }

or 要么

extern "C++" {
#include <stdint.h>
}
int main() { }

Is it specified what should happen in either case, or is it implementation-defined or even undefined? 是否指定了在两种情况下都应该发生的情况,还是实现定义的甚至是未定义的?

C++11 17.6.2.3p1 says that C++ standard library headers put stuff in extern "C++" , but my tentative reading is that this doesn't apply to C headers like <stdint.h> . C ++ 11 17.6.2.3p1表示C ++标准库标头将内容放在extern "C++" ,但我的初步理解是,这不适用于<stdint.h>类的C标头。 C++11 17.6.2.2p3 says headers can only be #include d outside of any "external declaration"; C ++ 11 17.6.2.2p3表示标头只能在任何“外部声明”之外#include d; this phrase appears only this one place in C++11, so I'm not sure if it might apply here. 这个短语在C ++ 11中仅出现在这一位置,所以我不确定它是否可能在这里适用。 (I'm assuming C99 has nothing to say about this.) (我假设C99对此无话可说。)

(For my particular case it isn't an option to use C++'s <c*> standard headers, so I really need to know the semantics only for the old-school C headers.) (对于我的特殊情况,不能选择使用C ++的<c*>标准标头,因此我真的只需要了解老式C标头的语义。)

The standard doesn't say anything about C headers that are not part of C++. 该标准没有说明不属于C ++的C头文件。 Previous to C++-11, stdint.h / cstdint was not part of C++. 在C ++-11之前, stdint.h / cstdint不属于C ++。 It's up to that header how it behaves when included from C++ code. 由C ++代码包含时,它的行为取决于该标头。

If you're asking about C headers that are part of C++, the only difference between *.h and c* is that the former isn't required to add its identifiers to the std namespace (it's optional whether it does so or not,) while the latter is required to do so (and it can optionally also add them to the global namespace.) There's no other difference. 如果您要问的 C ++中的C标头, *.hc*之间的唯一区别是不需要前者将其标识符添加到std名称空间中(是否这样做是可选的, ),而后者则需要这样做(它也可以选择将它们添加到全局名称空间中。)没有其他区别。 You shouldn't include a standard *.h header inside an extern "C" block, as the headers themselves take care of using C linkage where needed. 您不应该在extern "C"块中包含标准的*.h标头,因为标头本身需要在需要的地方使用C链接。

If you have non-standard C headers you want to include from C++ code, then you must inspect those headers to determine whether you need to include them extern "C" or not on a case by case basis. 如果您要从C ++代码中包含非标准的C标头,则必须检查这些标头,以确定是否需要视情况将它们包含在extern "C"

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

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