标签: preprocessor-directive

预处理器指令"使用"

我以前在学习知识Ç,我知道预处理指令一样#include,#define是不是这就是为什么顾名思义,它是过程编译程序之前的声明,因此没有必要为我们追加;在结束它.

在C++中,它向我介绍了一个新的指令using,但为什么这个指令会附加一个分号?我认为这就像我之前的指令,我知道它不是一个声明?

c++ c-preprocessor preprocessor-directive

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

在预处理程序指令中使用变量

哪个全局变量可以在预处理程序指令file.cpp中使用

int variable = 1;
#if variable >= 1
    int a = 0;
#else 
    int a = 1;
#endif
Run Code Online (Sandbox Code Playgroud)

要么

file.cpp

const int variable = 1;
#if variable >= 1
    int a = 0;
#else 
    int a = 1;
#endif
Run Code Online (Sandbox Code Playgroud)

或file.cpp

#include "header.h"
// extern in variable; in the header.h
#if variable >= 1
    int a = 0;
#else 
    int a = 1;
#endif
Run Code Online (Sandbox Code Playgroud)

使用proprocessor指令中的变量的规则是什么?如果一个可以折叠的变量,它可以用在#if /#elif#else指令中吗?

c c++ preprocessor-directive

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

许多功能的相同模板

大家好(祝新年快乐!)

我正在用C++写一个(不是真的)简单的项目(我的第一个,来自普通的C).我想知道是否有办法简化具有相同模板模式的多个函数的定义.我想一个例子可以更好地解释这个问题.

上下文

假设我有一个"Set"类,它代表一个数字列表,定义为

template <class T>
class Set {
    static_assert(std::is_arithmetic<T>(), "Template argument must be an arithmetic type.");
    T *_address;
    ...
}
Run Code Online (Sandbox Code Playgroud)

所以,如果我有一个实例(比如说Set<double>)和一个数组U array[N],其中U是另一个算术类型并且N是一个整数,我希望能够执行一些操作,例如将数组的值赋给那些数组Set.因此我在类中创建了函数模板

template <class U, int N>
void assign(U (&s)[N]) {
    static_assert(std::is_arithmetic<U>(), "Template argument must be an arithmetic type.");
    errlog(E_BAD_ARRAY_SIZE, N == _size);
    idx_t i = 0;
    do {
        _address[i] = value[i];
    } while (++i < size);
}
Run Code Online (Sandbox Code Playgroud)

问题

至于我的测试,上面的代码完全正常.但是我发现真的很难看,因为我需要static_assert确保只将算术类型作为参数(参数U),我需要一种方法来确保数组大小(参数N).另外,我不与完成assign的功能,但我需要这么多的其他功能,如 …

c++ templates preprocessor-directive

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

仅在_DEBUG定义时打印:c

我想只在_DEBUG定义时打印信息

#define DEBUG(y) y == true ? #define _DEBUG true  : #define _DEBUG false

#ifdef _DEBUG
#define Print(s)  printf(s); 
#endif
Run Code Online (Sandbox Code Playgroud)

得到错误:

error: '#' is not followed by a macro parameter
Run Code Online (Sandbox Code Playgroud)

有任何建议如何使用预处理器指令实现这一目标?

我打算从我的主要使用它:

DEBUG(true);
Print("Inside main in debug mode");
Run Code Online (Sandbox Code Playgroud)

c preprocessor-directive

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

C / C ++宏,用于中断或继续

我正在尝试编写一个简单的宏,该宏基于调用的条件breakcontinue在调用它的循环中的条件。下面是代码:

#include <iostream>

#define BC_IF_EVEN(BC) if(i % 2 == 0) BC

using namespace std;

int main() {
    int i = 0;
    while(i++ < 30) {
            if(i < 15)
                    BC_IF_EVEN(continue);
            else
                    BC_IF_EVEN(break);

            cout << i << " ";
    }
    cout << endl;
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找的输出是:1 3 5 7 9 11 13 15,但是上面的代码输出:1 3 5 7 9 11 13 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30因为 …

c c++ c-preprocessor preprocessor-directive

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

我们是否应该在.h和.cpp文件中包含一个宏(#define)

如果我在两个文件中都使用它们.在c ++中使用术语"宏"它只是在预编译阶段发生的替换机制(对c ++结构一无所知).

这是我应该在两个文件中定义宏(#define)还是不需要这样做的原因,或者我错过了什么.

c++ preprocessor-directive

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

#define与运营商一起使用

我知道它#define具有以下语法:#define SYMBOL string 例如,如果我写

#define ALPHA 2-1
#define BETA ALPHA*2
Run Code Online (Sandbox Code Playgroud)

然后ALPHA = 1BETA = 0.(为什么?)

但如果我写这样的东西

#define ALPHA (2-1)
#define BETA ALPHA*2
Run Code Online (Sandbox Code Playgroud)

然后ALPHA = 1BETA = 2.

有人可以解释一下这两者之间有什么区别吗?

c++ preprocessor-directive

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

扩展 C 宏时的“预期主表达式”

我正在尝试默认初始化一个配置结构,该结构由一些字段组成,其中包括另一个从属配置结构 - 使用宏:

现场演示

#include <cstdio>

#define MYCLASS_DEFAULT_CONFIG mylib::options { \
    .a_ = 2, \
    .b_ = 3, \
    .subopts_ = MYCLASS_DEFAULT_SUBOPT_CONFIG() \
}

#define MYCLASS_DEFAULT_SUBOPT_CONFIG mylib::sub_options { \
    .c_ = 'A'; \
    .d_ = 'H'; \
}


namespace mylib
{
    struct sub_options
    {
        char c_;
        char d_;
    };

    struct options
    {
        int a_;
        int b_;
        sub_options subopts_;
    };


    class myclass
    {
        myclass(options opts)
            : opts_ { opts }
        {
            
        }

        options opts_;
    };
}

int main()
{
    mylib::myclass some_class(MYCLASS_DEFAULT_CONFIG()); …
Run Code Online (Sandbox Code Playgroud)

c++ struct aggregate c-preprocessor preprocessor-directive

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

C++ - 如何在宏内使用预处理器 if 语句(#if、#elif、#endif)?

我目前正在开发一个我打算移植的程序。我可以访问 Windows 和 macOS,并且我希望能够在两者上轻松调试。当错误处理时,我希望在那里有调试中断以使其变得容易(__debugbreak()对于 MSVC)。由于我打算在多个平台上进行开发和测试,因此我想制作一个宏来执行以下操作:

#define DEBUG_BREAK #ifdef DEBUG\
    #if _MSC_VER                \
        __debugbreak();         \
    #elif __GNUC__              \
        __builtin_trap();       \
    #endif                      \
#endif
Run Code Online (Sandbox Code Playgroud)

DEBUG_BREAK因此,在任何平台上调试时,我可以在任何想要破坏代码的地方编写代码。当我使用这个宏时,我收到错误'#' not expected here

我发现两个有些相关的问题:

  1. 如何在 C 预处理器中的#define 中使用#if?
  2. C 预处理器 #if 表达式

但他们都没有回答我的问题,因为他们试图完成不同的事情。

所以我的问题是:如果允许的话,如何在宏内部使用预处理器 if 语句?如果不可能,我该怎么做才能获得这个损坏的DEBUG_BREAK宏试图执行的相同功能?

注:DEBUG编译时定义,用于调试;编译发布时未定义它。

c++ macros visual-c++ preprocessor-directive

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

在 C 中定义自定义语法“until”

我希望我可以做这样的事情:

int i = 0;
until (i == 2){
  printf("yes\n");
  i++;
}
Run Code Online (Sandbox Code Playgroud)

在不详细说明具体内容的until情况下,我确信读者从上面的代码中知道算法是什么。是的,我知道我可以使用while(!condition){}.

输出将是:

yes
yes
Run Code Online (Sandbox Code Playgroud)

那么我有可能实现我的目标吗?

我觉得宏可以用define其他东西来做到这一点。但我缺乏关于 C 中预处理指令语法的知识

#define until <what should I fill here>
Run Code Online (Sandbox Code Playgroud)

编辑:很多人都是因为我在做什么而触发的。如果打扰你们了,我很抱歉。别担心,这个语法仅供我自己使用。所以我希望不要打扰那些不小心读到我代码的代码读者或者C牧师。

c syntax macros preprocessor-directive

0
推荐指数
2
解决办法
420
查看次数

用于处理编译错误的C/C++预处理程序指令

标题可能有点令人困惑,所以我会试着解释一下.

是否有一个预处理器指令,我可以用一段代码封装,如果这段代码包含编译错误,那么应该编译其他一些代码?

这是一个说明我的动机的例子:

#compile_if_ok
    int a = 5;
    a += 6;
    int b = 7;
    b += 8;
#else
    int a = 5;
    int b = 7;
    a += 6;
    b += 8;
#endif
Run Code Online (Sandbox Code Playgroud)

以上示例不是我正在处理的问题,因此请不要提出具体的解决方案.

更新:

感谢那里的所有负面评论.

这是确切的问题,也许有一点负面方法的人会得到答案:

我试图在编译时决定某个变量a是数组还是指针.

我想我可以使用这样一个事实:与指针不同,数组没有L值.

因此,实质上,以下代码会产生数组的编译错误,但不会产生指针:

int a[10];
a = (int*)5;
Run Code Online (Sandbox Code Playgroud)

我可以以某种方式"利用"这个编译错误,以确定这a是一个数组而不是一个指针,而不停止编译过程

谢谢

c c++ preprocessor-directive

-2
推荐指数
1
解决办法
259
查看次数

预处理程序命令中的sizeof不会编译,错误为C1017

我想使用预处理器命令来控制代码执行路径。因为这样可以节省运行时间。

#if (sizeof(T)==1 不符合错误:C1017

template<typename T>
    class String
    {
    public:
        static void showSize()
        {
#if (sizeof(T)==1) 
            cout << "char\n";
#else
            cout << "wchar_t\n";
#endif
        }
    };

    inline void test()
    {
        String<char>::showSize();
        String<wchar_t>::showSize();
    }
Run Code Online (Sandbox Code Playgroud)

c++ preprocessor-directive

-4
推荐指数
1
解决办法
77
查看次数

标记“(”之前缺少二元运算符

错误:

cxx.cpp:5:13: error: missing binary operator before token "("
cxx.cpp:7:15: error: missing binary operator before token "("
Run Code Online (Sandbox Code Playgroud)

代码:

  #if definied(_WIN32) || definied(_WIN64) || definied(__WIN32__)
        const char * PORT = "COM1";
    #elif definied(__linux) || definied(__linux__) || definied(linux)
        const char * PORT = "dev/ttyS1";
    #else 
        const char * PORT = NULL;
    #endif
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 编译器正在等待新的defined()调用?
  2. 它可以检测到任何 linux(和变体)或 windows 版本?

提前致谢。

c++ g++ preprocessor-directive

-7
推荐指数
1
解决办法
5810
查看次数