标签: c-preprocessor

为什么当我对 int 使用 #define 时,我需要将它们括在括号中?

这是我发现的例子:

#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前面又是什么意思?有什么指南吗?

objective-c ios c-preprocessor

0
推荐指数
1
解决办法
226
查看次数

在编译时创建一个标识符,其中包含行号

我想要一个 C++ 预处理器宏,它创建一个新的标识符,其中包含行号。这是为了将标识符用作不与任何其他变量名称冲突的一次性变量。例如,如果我写

VARNAME("Var")
Run Code Online (Sandbox Code Playgroud)

在文件的第 100 行,我希望预处理器生成变量名:

Var100
Run Code Online (Sandbox Code Playgroud)

请问我该怎么做?我知道我必须使用字符串化和__LINE__预定义的宏,但我无法弄清楚如何将它们组合在一起。

c++ c-preprocessor

0
推荐指数
1
解决办法
320
查看次数

我们可以在多行中编写一个没有反斜杠的宏吗?

我在 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 处理宏参数中的任意指令的方式与它处理指令的方式完全相同,如果不存在类似函数的宏调用。

如果在宏调用中,该宏被重新定义,则新定义在参数预扩展时及时生效,但原始定义仍用于参数替换。这是一个病理学例子:

 #define f(x) x x
 f (1
 #undef f
 #define f 2
 f)
Run Code Online (Sandbox Code Playgroud)

扩展到

 1 2 1 2
Run Code Online (Sandbox Code Playgroud)

具有上述语义。

这个例子有很多行。

c gcc c-preprocessor

0
推荐指数
1
解决办法
3170
查看次数

宏的类型转换以优化代码

致力于优化代码。将宏类型转换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)

c macros c-preprocessor

0
推荐指数
1
解决办法
2312
查看次数

为什么预处理器用Borland C ++标识枚举值,而不能用Visual C ++标识枚举值?

我正在尝试将项目从迁移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)

c++ enums visual-c++ c-preprocessor borland-c++

0
推荐指数
1
解决办法
212
查看次数

g ++ 4.8.5:未使用##给出有效的预处理令牌

当我尝试在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++ preprocessor g++ c-preprocessor

0
推荐指数
1
解决办法
67
查看次数

使用 C 语法在一行中重新定义宏常量的方法?

我想在我的 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)

c macros c-preprocessor

0
推荐指数
1
解决办法
113
查看次数

如何将宏参数插入变量 C++ 处理器?

我想使用宏将参数传递给变量名称。

例如,我会有这个代码:

#define FOO(I,J) double varIJiable

FOO(1,2);
Run Code Online (Sandbox Code Playgroud)

预处理器会输出这个:

double var12iable;
Run Code Online (Sandbox Code Playgroud)

c++ c-preprocessor

0
推荐指数
1
解决办法
52
查看次数

当别名定义为“const int alias = variable”而不是#define时使用ifndef时重新定义错误

我定义const UInt8 HE = he;里面namespace Portsports.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++ c-preprocessor ifndef

0
推荐指数
1
解决办法
80
查看次数

使用唯一的预处理器进行字符串连接?

是否可以连接语言之外的带引号的字符串文字(在本例中为 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 c-preprocessor

0
推荐指数
1
解决办法
71
查看次数

标签 统计

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