C / C ++预处理器宏可以有默认参数值吗? [重复]

时间:2022-11-25 10:53:39

This question already has an answer here:

这个问题在这里已有答案:

Can we specify default parameter values for macro parameters?

我们可以为宏参数指定默认参数值吗?

I know there isn't any type-checking, so I expect the default value to be nothing more than just some text used by the preprocessor for macro expansion in instances where the parameter value is not specified.

我知道没有任何类型检查,所以我希望默认值只不过是预处理器在未指定参数值的情况下用于宏扩展的一些文本。

3 个解决方案

#1


12  

You are looking for a macro overload mechanism which is provided in e.g. Boost.PP's facilities.

您正在寻找一种宏过载机制,例如在Boost.PP的设施。

#define MACRO_2(a, b) std::cout << a << ' ' << b;

#define MACRO_1(a) MACRO_2(a, "test") // Supply default argument

// Magic happens here:

#define MACRO(...) BOOST_PP_OVERLOAD(MACRO_, __VA_ARGS__)(__VA_ARGS__)

Demo. The number of arguments is concatenated with the macro name, which can easily be implemented without Boost as follows:

演示。参数的数量与宏名称连接,可以在没有Boost的情况下轻松实现,如下所示:

#define VARGS_(_10, _9, _8, _7, _6, _5, _4, _3, _2, _1, N, ...) N 
#define VARGS(...) VARGS_(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)

#define CONCAT_(a, b) a##b
#define CONCAT(a, b) CONCAT_(a, b)

And

#define MACRO_2(a, b) std::cout << a << ' ' << b;

#define MACRO_1(a) MACRO_2(a, "test") // Supply default argument

#define MACRO(...) CONCAT(MACRO_, VARGS(__VA_ARGS__))(__VA_ARGS__)

Demo.

#2


6  

There is a way to do it, if you permit an unusual syntax:

如果您允许不寻常的语法,有一种方法可以做到这一点:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

void TestInternal( int n )
{
    printf("%d\n" , n ) ;
}
#define TestGet( f , ... )  f
#define Test( ... ) TestInternal( TestGet( __VA_ARGS__ DEFAULT , DEFAULT ) )
#define DEFAULT 100

int main( void )
{ 
    Test( ) ;
    Test( 12345 , ) ;

return 0 ;
}

This is type safe and can be expanded to any number of arguments.

这是类型安全的,可以扩展为任意数量的参数。

Note that the comma must be present if you pass a variable. You can treat it as an indicator that this function/macro has default parameters.

请注意,如果传递变量,则必须存在逗号。您可以将其视为此函数/宏具有默认参数的指示符。

Using a similar version you can omit the comma, but then your functions must have at least one non-default parameter which must be first.

使用类似的版本可以省略逗号,但是你的函数必须至少有一个非默认参数必须是第一个。

#3


1  

It seems like the simplest solution is to define an additional macro that is missing the default parameter.

似乎最简单的解决方案是定义一个缺少默认参数的附加宏。

#define MACRO1( PARAM1 , PARAM2 ) &( PARAM1 + PARAM2 ) // or whatever logic

#define MACRO1_DEFAULT( PARAM1 ) MACRO1 ( PARAM1 , 12 ) // PARAM2 default of 12

Also, as mentioned by 40two, the language does support using an ellipsis for variadic macros, which may also be a viable solution in some instances.

此外,如40two所述,该语言确实支持对可变参数宏使用省略号,这在某些情况下也可能是可行的解决方案。

Contents of the linked article:

链接文章的内容:

Variadic macros are function-like macros that contain a variable number of arguments.

变量宏是类似函数的宏,包含可变数量的参数。

To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments.
__VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.

要使用可变参数宏,可以将省略号指定为宏定义中的最终形式参数,并且可以在定义中使用替换标识符__VA_ARGS__来插入额外参数。 __VA_ARGS__将替换为与省略号匹配的所有参数,包括它们之间的逗号。

The C Standard specifies that at least one argument must be passed to the ellipsis, to ensure that the macro does not resolve to an expression with a trailing comma.

C标准指定必须至少将一个参数传递给省略号,以确保宏不会解析为具有尾随逗号的表达式。

<Microsoft specific>
The Visual C++ implementation will suppress a trailing comma if no arguments are passed to the ellipsis.
</Microsoft Specific>

如果没有参数传递给省略号,Visual C ++实现将禁止尾随逗号。 特定>

Example

// variadic_macros.cpp
#include <stdio.h>
#define EMPTY

#define CHECK1(x, ...) if (!(x)) { printf(__VA_ARGS__); }
#define CHECK2(x, ...) if ((x)) { printf(__VA_ARGS__); }
#define CHECK3(...) { printf(__VA_ARGS__); }
#define MACRO(s, ...) printf(s, __VA_ARGS__)

int main() {
    CHECK1(0, "here %s %s %s", "are", "some", "varargs1(1)\n");
    CHECK1(1, "here %s %s %s", "are", "some", "varargs1(2)\n");   // won't print

    CHECK2(0, "here %s %s %s", "are", "some", "varargs2(3)\n");   // won't print
    CHECK2(1, "here %s %s %s", "are", "some", "varargs2(4)\n");

    // always invokes printf in the macro
    CHECK3("here %s %s %s", "are", "some", "varargs3(5)\n");

    MACRO("hello, world\n");

    MACRO("error\n", EMPTY); // would cause error C2059, except VC++ 
                             // suppresses the trailing comma
}

Output

here are some varargs1(1)
here are some varargs2(4)
here are some varargs3(5)
hello, world
error

#1


12  

You are looking for a macro overload mechanism which is provided in e.g. Boost.PP's facilities.

您正在寻找一种宏过载机制,例如在Boost.PP的设施。

#define MACRO_2(a, b) std::cout << a << ' ' << b;

#define MACRO_1(a) MACRO_2(a, "test") // Supply default argument

// Magic happens here:

#define MACRO(...) BOOST_PP_OVERLOAD(MACRO_, __VA_ARGS__)(__VA_ARGS__)

Demo. The number of arguments is concatenated with the macro name, which can easily be implemented without Boost as follows:

演示。参数的数量与宏名称连接,可以在没有Boost的情况下轻松实现,如下所示:

#define VARGS_(_10, _9, _8, _7, _6, _5, _4, _3, _2, _1, N, ...) N 
#define VARGS(...) VARGS_(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)

#define CONCAT_(a, b) a##b
#define CONCAT(a, b) CONCAT_(a, b)

And

#define MACRO_2(a, b) std::cout << a << ' ' << b;

#define MACRO_1(a) MACRO_2(a, "test") // Supply default argument

#define MACRO(...) CONCAT(MACRO_, VARGS(__VA_ARGS__))(__VA_ARGS__)

Demo.

#2


6  

There is a way to do it, if you permit an unusual syntax:

如果您允许不寻常的语法,有一种方法可以做到这一点:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

void TestInternal( int n )
{
    printf("%d\n" , n ) ;
}
#define TestGet( f , ... )  f
#define Test( ... ) TestInternal( TestGet( __VA_ARGS__ DEFAULT , DEFAULT ) )
#define DEFAULT 100

int main( void )
{ 
    Test( ) ;
    Test( 12345 , ) ;

return 0 ;
}

This is type safe and can be expanded to any number of arguments.

这是类型安全的,可以扩展为任意数量的参数。

Note that the comma must be present if you pass a variable. You can treat it as an indicator that this function/macro has default parameters.

请注意,如果传递变量,则必须存在逗号。您可以将其视为此函数/宏具有默认参数的指示符。

Using a similar version you can omit the comma, but then your functions must have at least one non-default parameter which must be first.

使用类似的版本可以省略逗号,但是你的函数必须至少有一个非默认参数必须是第一个。

#3


1  

It seems like the simplest solution is to define an additional macro that is missing the default parameter.

似乎最简单的解决方案是定义一个缺少默认参数的附加宏。

#define MACRO1( PARAM1 , PARAM2 ) &( PARAM1 + PARAM2 ) // or whatever logic

#define MACRO1_DEFAULT( PARAM1 ) MACRO1 ( PARAM1 , 12 ) // PARAM2 default of 12

Also, as mentioned by 40two, the language does support using an ellipsis for variadic macros, which may also be a viable solution in some instances.

此外,如40two所述,该语言确实支持对可变参数宏使用省略号,这在某些情况下也可能是可行的解决方案。

Contents of the linked article:

链接文章的内容:

Variadic macros are function-like macros that contain a variable number of arguments.

变量宏是类似函数的宏,包含可变数量的参数。

To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments.
__VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.

要使用可变参数宏,可以将省略号指定为宏定义中的最终形式参数,并且可以在定义中使用替换标识符__VA_ARGS__来插入额外参数。 __VA_ARGS__将替换为与省略号匹配的所有参数,包括它们之间的逗号。

The C Standard specifies that at least one argument must be passed to the ellipsis, to ensure that the macro does not resolve to an expression with a trailing comma.

C标准指定必须至少将一个参数传递给省略号,以确保宏不会解析为具有尾随逗号的表达式。

<Microsoft specific>
The Visual C++ implementation will suppress a trailing comma if no arguments are passed to the ellipsis.
</Microsoft Specific>

如果没有参数传递给省略号,Visual C ++实现将禁止尾随逗号。 特定>

Example

// variadic_macros.cpp
#include <stdio.h>
#define EMPTY

#define CHECK1(x, ...) if (!(x)) { printf(__VA_ARGS__); }
#define CHECK2(x, ...) if ((x)) { printf(__VA_ARGS__); }
#define CHECK3(...) { printf(__VA_ARGS__); }
#define MACRO(s, ...) printf(s, __VA_ARGS__)

int main() {
    CHECK1(0, "here %s %s %s", "are", "some", "varargs1(1)\n");
    CHECK1(1, "here %s %s %s", "are", "some", "varargs1(2)\n");   // won't print

    CHECK2(0, "here %s %s %s", "are", "some", "varargs2(3)\n");   // won't print
    CHECK2(1, "here %s %s %s", "are", "some", "varargs2(4)\n");

    // always invokes printf in the macro
    CHECK3("here %s %s %s", "are", "some", "varargs3(5)\n");

    MACRO("hello, world\n");

    MACRO("error\n", EMPTY); // would cause error C2059, except VC++ 
                             // suppresses the trailing comma
}

Output

here are some varargs1(1)
here are some varargs2(4)
here are some varargs3(5)
hello, world
error