简体   繁体   English

在 C++ 中包括 C 标头作为 noexcept

[英]include C headers in C++ as noexcept

Is there a way(compiler extensions are acceptable) to include C headers and mark included C functions as noexcept , but without modifying headers?有没有办法(编译器扩展是可以接受的)包含 C 标头并将包含的 C 功能标记为noexcept ,但不修改标头?

For example, I have a C library and its header header.h .例如,我有一个 C 库及其 header header.h No C++ callback will be passed into it so it never throws.不会将 C++ 回调传递给它,因此它永远不会抛出。 Can I mark included C functions as noexcept or tell compiler that they never throw, so that the compiler doesn't have to generate unused code and enable some possible optimizations for callers.我可以将包含的 C 函数标记为noexcept或告诉编译器它们从不抛出,以便编译器不必生成未使用的代码并为调用者启用一些可能的优化。

extern "C" {
#include "header.h" // Is there a way to mark included C functions here as noexcept?
}

There isn't a way that you're looking for.没有您正在寻找的方法。

What you could do for example, is not use the header at all.例如,您可以做的是根本不使用 header。 Write your own header where the functions are marked noexcept.编写您自己的 header,其中函数标记为 noexcept。 This of course has the drawback that you must maintain the new header and keep it up to date with changes in the library.这当然有一个缺点,您必须维护新的 header 并使其与库中的更改保持同步。

Or - if you can give up the premise of not modifying the header - you can add noexcept to the header while still keeping it compatible with C by wrapping the C++ features in macros. Or - if you can give up the premise of not modifying the header - you can add noexcept to the header while still keeping it compatible with C by wrapping the C++ features in macros. This is how for example C standard libraries are written, as they are inherited by C++ standard libraries.这就是例如 C 标准库的编写方式,因为它们被 C++ 标准库继承。

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

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