C++ 我可以有条件地使用多个参数吗?

C++ 我可以有条件地使用多个参数吗?,c++,parameters,macros,parameter-passing,precompile,C++,Parameters,Macros,Parameter Passing,Precompile,因此,这段代码作为一个示例非常有效: #ifdef DEBUG #define DECLARE_DEBUG_PARAM(x) x #define PASS_DEBUG_PARAM(x) x #else #define DECLARE_DEBUG_PARAM(x) void #define PASS_DEBUG_PARAM(x) #endif int foo(DECLARE_DEBUG_PARAM(const bool param)) { #ifdef DEBUG if(param) {

因此,这段代码作为一个示例非常有效:

#ifdef DEBUG
#define DECLARE_DEBUG_PARAM(x) x
#define PASS_DEBUG_PARAM(x) x
#else
#define DECLARE_DEBUG_PARAM(x) void
#define PASS_DEBUG_PARAM(x)
#endif

int foo(DECLARE_DEBUG_PARAM(const bool param)) {
#ifdef DEBUG
    if(param) {
        cout << "DEBUG true\n";
    } else {
        cout << "DEBUG false\n";
    }
#else
    cout << "RETAIL\n";
#endif
}

int main() {
    foo(PASS_DEBUG_PARAM(true));
}
#ifdef调试
#定义DECLARE_DEBUG_PARAM(x)x
#定义PASS_DEBUG_参数(x)x
#否则
#定义DECLARE_DEBUG_PARAM(x)void
#定义PASS_DEBUG_参数(x)
#恩迪夫
intfoo(声明调试参数(常量布尔参数)){
#ifdef调试
如果(参数){

cout您应该在宏中包含逗号,而不是发出void

#ifdef DEBUG
#define DECLARE_DEBUG_PARAM(x) , x
#else
#define DECLARE_DEBUG_PARAM(x)
#endif
int foo(const int param1 DECLARE_DEBUG_PARAM(const int param2))
或者将逗号包含到参数中,以便此宏可以在任何地方使用:

#ifdef DEBUG
#define DECLARE_DEBUG_PARAM(...) __VA_ARGS__
#define PASS_DEBUG_PARAM(...) __VA_ARGS__
#else
#define DECLARE_DEBUG_PARAM(...)
#define PASS_DEBUG_PARAM(...)
#endif

int foo1(DECLARE_DEBUG_PARAM(const bool param)) {
#ifdef DEBUG
    if(param) {
        cout << "DEBUG true\n";
    } else {
        cout << "DEBUG false\n";
    }
#else
    cout << "RETAIL\n";
#endif
}

int foo2(int DECLARE_DEBUG_PARAM(, const bool param)) {
#ifdef DEBUG
    if(param) {
        cout << "DEBUG true\n";
    } else {
        cout << "DEBUG false\n";
    }
#else
    cout << "RETAIL\n";
#endif
}

int main() {

    foo1(PASS_DEBUG_PARAM(true));
    foo2(0 PASS_DEBUG_PARAM(,true));

    return 0;
}
#ifdef调试
#定义DECLARE_DEBUG_PARAM(…)u VA_参数__
#定义PASS_DEBUG_参数(…)\u VA_参数__
#否则
#定义DECLARE_DEBUG_参数(…)
#定义PASS_DEBUG_参数(…)
#恩迪夫
int foo1(声明调试参数(常量布尔参数)){
#ifdef调试
如果(参数){

cout您应该在宏中包含逗号,而不是发出void

#ifdef DEBUG
#define DECLARE_DEBUG_PARAM(x) , x
#else
#define DECLARE_DEBUG_PARAM(x)
#endif
int foo(const int param1 DECLARE_DEBUG_PARAM(const int param2))
或者将逗号包含到参数中,以便此宏可以在任何地方使用:

#ifdef DEBUG
#define DECLARE_DEBUG_PARAM(...) __VA_ARGS__
#define PASS_DEBUG_PARAM(...) __VA_ARGS__
#else
#define DECLARE_DEBUG_PARAM(...)
#define PASS_DEBUG_PARAM(...)
#endif

int foo1(DECLARE_DEBUG_PARAM(const bool param)) {
#ifdef DEBUG
    if(param) {
        cout << "DEBUG true\n";
    } else {
        cout << "DEBUG false\n";
    }
#else
    cout << "RETAIL\n";
#endif
}

int foo2(int DECLARE_DEBUG_PARAM(, const bool param)) {
#ifdef DEBUG
    if(param) {
        cout << "DEBUG true\n";
    } else {
        cout << "DEBUG false\n";
    }
#else
    cout << "RETAIL\n";
#endif
}

int main() {

    foo1(PASS_DEBUG_PARAM(true));
    foo2(0 PASS_DEBUG_PARAM(,true));

    return 0;
}
#ifdef调试
#定义DECLARE_DEBUG_PARAM(…)u VA_参数__
#定义PASS_DEBUG_参数(…)\u VA_参数__
#否则
#定义DECLARE_DEBUG_参数(…)
#定义PASS_DEBUG_参数(…)
#恩迪夫
int foo1(声明调试参数(常量布尔参数)){
#ifdef调试
如果(参数){
cout
我会建议一些不同的选择


1) 使用默认值:

enum类DebugSwitch{No_debug,debug}
void函数(int param1,DebugSwitch debug_param=DebugSwitch::No_debug)
{...}
2) 使用参数对象

struct参数
{
int参数1;
布尔调试参数;
// ....
};
空洞函数(参数和p)
{
///...
}
为什么?您正在乘以可发送到函数的可能值组合的数量

DEBUG defined+param==true
调试定义+参数==false
调试未定义+param==true
调试未定义+param==false
为确保正确处理所有组合,请减少“转向”变量的数量。

我会建议一些不同的选择


1) 使用默认值:

enum类DebugSwitch{No_debug,debug}
void函数(int param1,DebugSwitch debug_param=DebugSwitch::No_debug)
{...}
2) 使用参数对象

struct参数
{
int参数1;
布尔调试参数;
// ....
};
空洞函数(参数和p)
{
///...
}
为什么?您正在乘以可发送到函数的可能值组合的数量

DEBUG defined+param==true
调试定义+参数==false
调试未定义+param==true
调试未定义+param==false

确保你正确处理所有的组合-减少“转向”变量的数量。< /P> < P>引用C++ FAQ:

因为
#define
宏有四种不同的邪恶方式:、、和。有时你无论如何都应该使用它们,但它们仍然邪恶

在这里使用宏是没有意义的。它只是
\ifdef
输出代码,但需要用户查找。更好的方法是使用
\ifdef

int foo(
#ifdef DEBUG
        const bool param
#endif
       );
int foo(const int param1
#ifdef DEBUG
        , const int param2
#endif
       );
打电话做同样的事情:

foo(
#ifdef DEBUG
    true
#endif
   );
foo(13
#ifdef DEBUG
    , 42
#endif
   );
对于采用少量参数的函数,此代码可能会有点混乱,如果认为可读性更好,则可以使用
#else
进行布局:

#ifdef DEBUG
    int foo(const bool param);
#else
    int foo();
#endif
#ifdef DEBUG
    int foo(const int param1, const int param2);
#else
    int foo(const int param1);
#endif
对于呼叫,也可以执行相同的操作:

#ifdef DEBUG
    foo(true);
#else
    foo();
#endif
#ifdef DEBUG
    foo(13, 42);
#else
    foo(13);
#endif

引用C++常见问题解答:< /P> 因为

#define
宏有四种不同的邪恶方式:、、和。有时你无论如何都应该使用它们,但它们仍然邪恶

在这里使用宏是没有意义的。它只是
\ifdef
输出代码,但需要用户查找。更好的方法是使用
\ifdef

int foo(
#ifdef DEBUG
        const bool param
#endif
       );
int foo(const int param1
#ifdef DEBUG
        , const int param2
#endif
       );
打电话做同样的事情:

foo(
#ifdef DEBUG
    true
#endif
   );
foo(13
#ifdef DEBUG
    , 42
#endif
   );
对于采用少量参数的函数,此代码可能会有点混乱,如果认为可读性更好,则可以使用
#else
进行布局:

#ifdef DEBUG
    int foo(const bool param);
#else
    int foo();
#endif
#ifdef DEBUG
    int foo(const int param1, const int param2);
#else
    int foo(const int param1);
#endif
对于呼叫,也可以执行相同的操作:

#ifdef DEBUG
    foo(true);
#else
    foo();
#endif
#ifdef DEBUG
    foo(13, 42);
#else
    foo(13);
#endif

是的,看起来内部处理逗号的东西将是唯一的方法。但我不认为这是一个彻底的解决方案,因为如果它是唯一的参数会怎么样。我可能需要用户根据需要在内部传递逗号。使用
\uu VA\u ARGS\uuu
,您可能能够编写
int foo(const int param1 DECLARE_DEBUG_PARAM(,int param2))
使用0或n个参数处理
foo
。是的,看起来内部处理逗号的方法将是唯一的方法。但我不认为这是一个彻底的解决方案,因为如果它是唯一的参数,会发生什么。我可能需要用户根据需要在内部传递逗号。使用
\u VA\AR在GS_uu
中,您可以编写
int-foo(const int-param1 DECLARE_DEBUG_-PARAM(,int-param2))
来处理带有0或n个参数的
foo