更改 C(或 C++)中的宏
Changing a macro in C (or C++)
换句话说,如何根据宏的先前定义重新定义宏?具体来说,我想在 C++ 中的字符串宏的末尾添加一个字符串。这是我到目前为止尝试过的:
#define FOO "bla"
// code here will read FOO as "bla"
#define FOO FOO ## "dder"
// code here will read FOO as "bladder"
在 C++ 中,这个 returns error: FOO was not declared in this scope
。我该怎么做而不会出现此错误?
编辑:
我阅读了评论并发现了“XY 问题”是什么。我想做的是为 C 创建一个库,但该库是用 C++ 编写的。因为该库需要 类 才能运行(因为它使用其他 C++ 库),所以我想创建一个空的 class,使用该库的 C 程序可以添加自定义属性以使用宏函数。像这样:
// lib.h
#define PROPERTIES ""
#define NEW_PROPERTY(X) // add X + ";\n" to PROPERTIES (somehow)
#ifdef __cplusplus
Class myclass
{
public:
PROPERTIES
}
#endif
_
// c code
#include <lib.h>
NEW_PROPERTY(int id);
NEW_PROPERTY(int color);
int main(){
...
c 程序不需要访问这些属性,因为它们的存在只是为了让作为我的库的依赖项的第三方库可以使用它们。
这是不可能的。
来自gcc documentation(重点是我的):
However, if an identifier which is currently a macro is redefined, then the new definition must be effectively the same as the old one. Two macro definitions are effectively the same if:
- Both are the same type of macro (object- or function-like).
- All the tokens of the replacement list are the same.
- If there are any parameters, they are the same.
- Whitespace appears in the same places in both. It need not be exactly the same amount of whitespace, though. Remember that comments count as whitespace.
These definitions are effectively the same:
#define FOUR (2 + 2)
#define FOUR (2 + 2)
#define FOUR (2 /* two */ + 2)
but these are not:
#define FOUR (2 + 2)
#define FOUR ( 2+2 )
#define FOUR (2 * 2)
#define FOUR(score,and,seven,years,ago) (2 + 2)
If a macro is redefined with a definition that is not effectively the same as the old one, the preprocessor issues a warning and changes the macro to use the new definition. If the new definition is effectively the same, the redefinition is silently ignored. This allows, for instance, two different headers to define a common macro. The preprocessor will only complain if the definitions do not match.
换句话说,如何根据宏的先前定义重新定义宏?具体来说,我想在 C++ 中的字符串宏的末尾添加一个字符串。这是我到目前为止尝试过的:
#define FOO "bla"
// code here will read FOO as "bla"
#define FOO FOO ## "dder"
// code here will read FOO as "bladder"
在 C++ 中,这个 returns error: FOO was not declared in this scope
。我该怎么做而不会出现此错误?
编辑: 我阅读了评论并发现了“XY 问题”是什么。我想做的是为 C 创建一个库,但该库是用 C++ 编写的。因为该库需要 类 才能运行(因为它使用其他 C++ 库),所以我想创建一个空的 class,使用该库的 C 程序可以添加自定义属性以使用宏函数。像这样:
// lib.h
#define PROPERTIES ""
#define NEW_PROPERTY(X) // add X + ";\n" to PROPERTIES (somehow)
#ifdef __cplusplus
Class myclass
{
public:
PROPERTIES
}
#endif
_
// c code
#include <lib.h>
NEW_PROPERTY(int id);
NEW_PROPERTY(int color);
int main(){
...
c 程序不需要访问这些属性,因为它们的存在只是为了让作为我的库的依赖项的第三方库可以使用它们。
这是不可能的。
来自gcc documentation(重点是我的):
However, if an identifier which is currently a macro is redefined, then the new definition must be effectively the same as the old one. Two macro definitions are effectively the same if:
- Both are the same type of macro (object- or function-like).
- All the tokens of the replacement list are the same.
- If there are any parameters, they are the same.
- Whitespace appears in the same places in both. It need not be exactly the same amount of whitespace, though. Remember that comments count as whitespace.
These definitions are effectively the same:
#define FOUR (2 + 2) #define FOUR (2 + 2) #define FOUR (2 /* two */ + 2)
but these are not:
#define FOUR (2 + 2) #define FOUR ( 2+2 ) #define FOUR (2 * 2) #define FOUR(score,and,seven,years,ago) (2 + 2)
If a macro is redefined with a definition that is not effectively the same as the old one, the preprocessor issues a warning and changes the macro to use the new definition. If the new definition is effectively the same, the redefinition is silently ignored. This allows, for instance, two different headers to define a common macro. The preprocessor will only complain if the definitions do not match.