定义宏中的杂注

发布于 2024-09-06 02:53:39 字数 370 浏览 6 评论 0原文

有没有办法将 pragma 语句与其他语句一起嵌入到宏中?

我试图实现类似的目标:

#define DEFINE_DELETE_OBJECT(type)                      \
    void delete_ ## type_(int handle);                  \
    void delete_ ## type(int handle);                                                \
    #pragma weak delete_ ## type_ = delete_ ## type

如果存在的话,我可以接受升压解决方案(除了波)。

Is there some way to embed pragma statement in macro with other statements?

I am trying to achieve something like:

#define DEFINE_DELETE_OBJECT(type)                      \
    void delete_ ## type_(int handle);                  \
    void delete_ ## type(int handle);                                                \
    #pragma weak delete_ ## type_ = delete_ ## type

I am okay with boost solutions (save for wave) if one exists.

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

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

发布评论

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

评论(4

忆悲凉 2024-09-13 02:53:39

如果您使用的是 c99 或 c++0x,则有 pragma 运算符,使用 as

_Pragma("argument")

等同于

#pragma argument

except 它可以在宏中使用(请参阅 c99 标准的第 6.10.9 节,或 c++0x 最终的 16.9委员会草案)

例如,

#define STRINGIFY(a) #a
#define DEFINE_DELETE_OBJECT(type)                      \
    void delete_ ## type ## _(int handle);                  \
    void delete_ ## type(int handle);                   \
    _Pragma( STRINGIFY( weak delete_ ## type ## _ = delete_ ## type) )
DEFINE_DELETE_OBJECT(foo);

当放入 gcc -E 时给出

void delete_foo_(int handle); void delete_foo(int handle);
#pragma weak delete_foo_ = delete_foo
 ;

If you're using c99 or c++0x there is the pragma operator, used as

_Pragma("argument")

which is equivalent to

#pragma argument

except it can be used in macros (see section 6.10.9 of the c99 standard, or 16.9 of the c++0x final committee draft)

For example,

#define STRINGIFY(a) #a
#define DEFINE_DELETE_OBJECT(type)                      \
    void delete_ ## type ## _(int handle);                  \
    void delete_ ## type(int handle);                   \
    _Pragma( STRINGIFY( weak delete_ ## type ## _ = delete_ ## type) )
DEFINE_DELETE_OBJECT(foo);

when put into gcc -E gives

void delete_foo_(int handle); void delete_foo(int handle);
#pragma weak delete_foo_ = delete_foo
 ;
蛮可爱 2024-09-13 02:53:39

使用 _Pragma("argument") 可以做的一件好事是用它来处理一些编译器问题,例如

#ifdef _MSC_VER
#define DUMMY_PRAGMA _Pragma("argument")
#else
#define DUMMY_PRAGMA _Pragma("alt argument")
#endif

One nice thing you can do with _Pragma("argument") is use it to deal with some compiler issues such as

#ifdef _MSC_VER
#define DUMMY_PRAGMA _Pragma("argument")
#else
#define DUMMY_PRAGMA _Pragma("alt argument")
#endif
北风几吹夏 2024-09-13 02:53:39

不,没有可移植的方法可以做到这一点。话又说回来,根本没有可移植的方法来使用#pragma。因此,许多 C/C++ 编译器定义自己的方法来执行类似编译指示的操作,并且它们通常可以嵌入到宏中,但每个编译器都需要不同的宏定义。如果你愿意走这条路,你通常会做这样的事情:

#if defined(COMPILER_GCC)
#define Weak_b
#define Weak_e __attribute__((weak))
#elif defined(COMPILER_FOO)
#define Weak_b __Is_Weak
#define Weak_e
#endif

#define DEFINE_DELETE_OBJECT(type)                      \
    Weak_b void delete_ ## type_(int handle) Weak_e;    \
    Weak_b void delete_ ## type(int handle)  Weak_e;    

如果它不明显,你想将 Weak_bWeak_e 定义为开始和结束括号结构是因为一些编译器(例如 GCC)将属性添加为类型签名的附录,而另一些编译器(例如 MSC)将其添加为前缀(或者至少它做过一次,自从我使用 MSC 以来已经有很多年了)。使用括号结构可以让您定义始终有效的内容,即使您必须将整个类型签名传递到编译器构造中也是如此。

当然,如果您尝试将其移植到没有您想要的属性的编译器,那么除了让宏扩展为空并希望您的代码仍然运行之外,您无能为力。在纯粹警告或优化编译指示的情况下,这是可能的。在其他情况下,则没有那么多。

哦,我怀疑您实际上需要将 Weak_b 和 Weak_e 定义为带有参数的宏,但我不愿意通读文档以了解如何仅针对此示例创建弱定义。我把它留给读者作为练习。

No, there is no portable way of doing that. Then again, there are no portable ways to use #pragma at all. Because of this, many C/C++ compilers define their own methods for doing pragma-like things, and they often can be embedded in macros, but you need a different macro definition on every compiler. If you are willing to go that route, you often end up doing stuff like this:

#if defined(COMPILER_GCC)
#define Weak_b
#define Weak_e __attribute__((weak))
#elif defined(COMPILER_FOO)
#define Weak_b __Is_Weak
#define Weak_e
#endif

#define DEFINE_DELETE_OBJECT(type)                      \
    Weak_b void delete_ ## type_(int handle) Weak_e;    \
    Weak_b void delete_ ## type(int handle)  Weak_e;    

In case its not obvious you want to define Weak_b and Weak_e as begin-and-end bracketing constructs because some compilers like GCC add the attributes as an addendum to a type signature, and some, like MSC add it as a prefix (or at least it did once, its been years since I've used MSC). Having bracketing contructs allows you to define something that always works, even if you have to pass the entire type signature into a compiler construct.

Of course, if you try porting this to a compiler without the attributes you want, there's nothing you can do but leave the macros expand to nothing and hope your code still runs. In case of purely warning or optimizing pragmas, this is likely. In other cases, not so much.

Oh, and I suspect you'd actually need to define Weak_b and Weak_e as macros that take parameters, but I wasn't willing to read through the docs for how to create a weak definition just for this example. I leave that as an exercise for the reader.

记忆消瘦 2024-09-13 02:53:39

是否有某种方法可以将 pragma 语句与其他语句一起嵌入到宏中?

不可以,您不能将预处理器语句放入预处理器语句中。但是,您可以将其放入内联函数中。不过,这击败了 C 标签。

is there some way to embed pragma statement in macro with other statements?

No, you cannot put preprocessor statements into preprocessor statements. You could, however, put it into an inline function. That defeats the C tag, though.

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