问题描述
我刚刚发现,在将参数传递给宏时,不能总是使用括号初始化。当ASSERT()宏编译失败时,我发现了这一点。但是,以下示例说明了该问题:
I just discovered that you cannot always use brace initialization when passing arguments to macros. I found this when an ASSERT() macro failed to compile. However, the following example illustrates the problem:
#include <iostream>
#include <string>
using namespace std;
#define PRINT_SIZE( f ) cout << "Size=" << (f).size() << endl;
int main()
{
PRINT_SIZE( string("ABC") ); // OK, prints: "Size=3"
PRINT_SIZE( string{"ABC"} ); // OK, prints: "Size=3"
PRINT_SIZE( string("ABCDEF",3) ); // OK, prints: "Size=3"
PRINT_SIZE( string{"ABCDEF",3} ); // Error: macro 'PRINT_SIZE' passed 2 arguments, but takes just 1
return 0;
}
是否存在无法使宏用于括号初始化的原因?
Is there a reason why macros cannot be made to work with brace initialization?
编辑:
此后我发现您还可以使用可变参数宏,并且可以完美解决问题:
I have since discovered that you can also use a variadic macro, and that solves the problem perfectly:
#include <iostream>
#include <string>
using namespace std;
#define PRINT_SIZE( ... ) cout << "Size=" << (__VA_ARGS__).size() << endl;
int main()
{
PRINT_SIZE( string("ABC") ); // OK, prints: "Size=3"
PRINT_SIZE( string{"ABC"} ); // OK, prints: "Size=3"
PRINT_SIZE( string("ABCDEF",3) ); // OK, prints: "Size=3"
PRINT_SIZE( string{"ABCDEF",3} ); // OK, prints: "Size=3"
return 0;
}
推荐答案
列表分为几部分宏参数。
The list is split into several macro parameters. When you write
PRINT_SIZE( string{"ABCDEF",3} );
这会尝试使用以下命令扩展宏 PRINT_SIZE
两个参数,一个 string { ABCDEF
和一个 3}
失败。在许多情况下(包括您在内),可以通过添加另一对括号来解决此问题:
This attempts to expand the macro PRINT_SIZE
with two parameters, one string{"ABCDEF"
and one 3}
, which fails. This can be worked around in many cases (including yours) by adding another pair of parentheses:
PRINT_SIZE( (string{"ABCDEF",3}) );
这些括号可防止参数分裂,因此 PRINT_SIZE
用单个参数(string { ABCDEF,3})
扩展(请注意,括号是该参数的一部分)。
These parentheses prevent the splitting of the argument, so that PRINT_SIZE
is expanded with a single argument (string{"ABCDEF",3})
(note that the parentheses are part of the argument).
这篇关于为什么不能将C ++ 11大括号初始化与宏一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!