这是我发现的例子:
#define kNumberOfViews (37)
#define kViewsWide (5)
#define kViewMargin (2.0)
Run Code Online (Sandbox Code Playgroud)
为什么不能这样?
#define kNumberOfViews 37
#define kViewsWide 5
#define kViewMargin 2.0
Run Code Online (Sandbox Code Playgroud)
k前面又是什么意思?有什么指南吗?
我想要一个 C++ 预处理器宏,它创建一个新的标识符,其中包含行号。这是为了将标识符用作不与任何其他变量名称冲突的一次性变量。例如,如果我写
VARNAME("Var")
Run Code Online (Sandbox Code Playgroud)
在文件的第 100 行,我希望预处理器生成变量名:
Var100
Run Code Online (Sandbox Code Playgroud)
请问我该怎么做?我知道我必须使用字符串化和__LINE__预定义的宏,但我无法弄清楚如何将它们组合在一起。
我在 CPP 手册中看到了一些例子,我们可以在没有反斜杠的情况下在多行中编写宏主体。
#define strange(file) fprintf (file, "%s %d",
...
strange(stderr) p, 35)
Run Code Online (Sandbox Code Playgroud)
输出:
fprintf (stderr, "%s %d", p, 35)
Run Code Online (Sandbox Code Playgroud)
它们是像参数宏中的指令这样的特殊情况还是只允许用于 #define ?
对于包含指令,如果我没记错的话,它必须始终在一行中声明。
编辑:
来自https://gcc.gnu.org/onlinedocs/cpp/Directives-Within-Macro-Arguments.html
3.9 宏参数中的指令
有时,在宏的参数中使用预处理器指令会很方便。C 和 C++ 标准声明这些情况下的行为是未定义的。GNU CPP 处理宏参数中的任意指令的方式与它处理指令的方式完全相同,如果不存在类似函数的宏调用。
如果在宏调用中,该宏被重新定义,则新定义在参数预扩展时及时生效,但原始定义仍用于参数替换。这是一个病理学例子:
Run Code Online (Sandbox Code Playgroud)#define f(x) x x f (1 #undef f #define f 2 f)扩展到
Run Code Online (Sandbox Code Playgroud)1 2 1 2具有上述语义。
这个例子有很多行。
致力于优化代码。将宏类型转换char为减少内存消耗是个好主意吗?这样做可能有什么副作用?
例子:
#define TRUE 1 //non-optimized code
sizeof(TRUE) --> 4
#define TRUE 1 ((char) 0x01) //To optimize
sizeof(TRUE) --> 1
#define MAX 10 //non-optimized code
sizeof(MAX) --> 4
#define MAX ((char) 10) //To optimize
sizeof(MAX) --> 1
Run Code Online (Sandbox Code Playgroud) 我正在尝试将项目从迁移Borland C++到Visual C++
我注意到如本例所述在处理枚举方面存在差异
档案:Test_enum.cpp
#ifdef _MSC_VER
#include <iostream>
#else
#include <iostream.h>
#endif
#include <conio.h>
using namespace std;
enum {
ENUM_0 =0,
ENUM_1,
ENUM_2,
ENUM_3
} ;
int main(int argc, char* argv[])
{
#ifdef _MSC_VER
cout << "Microsoft Visual compiler detected!" << endl;
#elif defined(__BORLANDC__)
cout << "Borland compiler detected!" << endl;
#elif
cout << "Other compiler detected!" << endl;
#endif
#if ENUM_1 > 0
cout << "ENUM_1 is well defined at preprocessing time" << endl;
#else …Run Code Online (Sandbox Code Playgroud) 当我尝试在Linux中使用G ++ 4.8编译程序时出现错误“未提供有效的预处理令牌”。并且在Solaris中使用CCSuntudio进行编译时没有错误。
在我的代码下面:
#include <iostream>
#define func(type1,varname1) \
cout << "ma var est "<<##varname1<<" et le type est "<<#type1; \
cout <<endl;
using namespace std;
int main() {
func("int", "area");
}
Run Code Online (Sandbox Code Playgroud)
它可以在CCSunStudio中完美运行,但不适用于G ++
hello.hxx:2:23: error: pasting "<<" and ""area"" does not give a valid preprocessing token
cout << "ma var est "<<##varname1<<" et le type est "<<#type1; \
^
hello.cxx:7:1: note: in expansion of macro ‘func’
func("int","area");
^
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我想在我的 C 源代码中重新定义特定条件下宏常量的值。我无法使用条件预处理指令进行定义,LEN_OSG因为它是否用另一个值重新定义是在运行时决定的。
我已经拥有的是:
if(condition) // proof condition for replacing the value in LEN_OSG.
{
if(LEN_OSG != CBS_LEN) // check if LEN_OS already has the value in CBS_LEN.
{
#undef LEN_OSG // undefine macro LEN_OSG by using `#undef` directive.
#define LEN_OSG CBS_LEN // recreate the macro LEN_OSG with new value.
}
}
Run Code Online (Sandbox Code Playgroud)
C 语法是否允许另一种方法来缩写它,而不是使用该#undef指令,然后通过另一个#define指令(仅在一行中)使用新值重新创建宏?
所需的解决方案是这样的:
if(LEN_OSG != CBS_LEN) // check if LEN_OS already has the value in CBS_LEN.
{
#redefine LEN_OSG CBS_LEN // redefine …Run Code Online (Sandbox Code Playgroud) 我想使用宏将参数传递给变量名称。
例如,我会有这个代码:
#define FOO(I,J) double varIJiable
FOO(1,2);
Run Code Online (Sandbox Code Playgroud)
预处理器会输出这个:
double var12iable;
Run Code Online (Sandbox Code Playgroud) 我定义const UInt8 HE = he;里面namespace Ports在ports.h。然后我将它包含在ports_logic.h和 中ports_logic.h,我在里面有以下代码namespace Ports
#ifndef HP
const UInt8 HP = hp;
#endif
Run Code Online (Sandbox Code Playgroud)
但是在编译过程中,它给了我以下错误。
有什么替代方法ifndef可以帮助我检查是否const int HP已经定义?
谢谢。
是否可以连接语言之外的带引号的字符串文字(在本例中为 C++)?
也就是说,我可以这样定义MY_MACRO(a,b,c)和使用它:
MY_MACRO("one", "two", "three")
Run Code Online (Sandbox Code Playgroud)
并将其扩展为:"onetwothree"?
用例是将属性及其消息应用于函数签名,如下所示:
MY_ATTRIBUTE_MACRO("this", "is", "the reason") int foo() { return 99; }
Run Code Online (Sandbox Code Playgroud)
这将导致:
[[nodiscard("thisisthe reason")]] int foo() { return 99; }
Run Code Online (Sandbox Code Playgroud) c-preprocessor ×10
c++ ×6
c ×3
macros ×2
preprocessor ×2
borland-c++ ×1
enums ×1
g++ ×1
gcc ×1
ifndef ×1
ios ×1
objective-c ×1
visual-c++ ×1