问题描述
我猜这个问题的答案是否定的,但如果有办法就太好了.为了澄清,假设我有以下宏:
My guess is the answer to this question is no, but it would be awesome if there was a way. To clarify, assume I have the following macro:
#define MY_VARIADIC_MACRO(X...) // Does some stuff here in the macro definition
我想做的是在将 X 的所有变量传递给可变参数函数之前以某种方式对其进行字符串化;这里的关键字是之前.我意识到无法从宏定义中真正访问各个参数,但是有没有办法将所有参数字符串化,可能类似于以下内容?
What I would like to do is somehow perform stringification on all the variables of X before passing it to a variadic function; the keyword here is before. I realize there's no way to really access the individual arguments from within the macro definition, but is there a way to stringify all the arguments, with maybe something like the following?
#define MY_VARIADIC_MACRO(X...) some_variadic_function("some string", #X)
推荐答案
好吧,我不是想在这里回答我自己的问题,但我想出了一个不错的解决方案,有点像 Mark Wilkins 的组合答案和我在问题中给出的例子.
Okay, I didn't mean to answer my own question here, but I've come up with a decent solution that is somewhat of a combination of Mark Wilkins answer and the example I gave in the question.
可以对整个可变参数集进行字符串化,然后在字符串中包含分隔逗号.这是一个简单的例子:
It is possible to stringify the entire set variadic set, which then includes the delimiting commas in the string. Here's a quick example:
#define MY_VARIADIC_MACRO(X...) printf(#X)
使用上面的宏表明传递给宏的整个参数集都被字符串化了.
Using the above macro shows you that the entire set of arguments passed to the macro gets stringified.
然后您可以定义一个函数来使用分隔逗号对这些参数进行标记,从而通过使用可变参数宏获得一组标记的字符串:
Then you can then define a function to tokenize these arguments using the delimiting comma, thereby getting the set of tokenized strings by using the variadic macro:
#define MY_VARIADIC_MACRO(X...) tokenize_my_arguments(#X)
然后实际上不再依赖于让可变参数宏调用可变参数函数,我可以很好地遍历我的常量 C 字符串数组,而不是遍历 va_arg.
Then there's actually no longer the dependency of having the variadic macro call a variadic function and I can iterate nicely through my array of constant C strings rather than iterating through va_arg.
* 来自编辑关注的新内容*
* New Stuff from Edit Follows *
根据 Tim 的评论,这里是解决方案的详细信息.请原谅任何错误,因为它是匆忙完成的,我不得不从我正在做的事情中移植过来.此外,它并不意味着是复制/粘贴解决方案,因为它只输出参数的字符串化来演示 POC,但应该足以演示功能.
Per Tim's comment, here's the details of the solution. Please forgive any errors since it was done in haste and I had to port from what I'm working on. Also, it's not meant to be copy/paste solution since it only outputs the stringification of the arguments to demonstrate POC, but should be sufficient enough to demonstrate the functionality.
虽然这个解决方案需要一些运行时计算,可变参数宏经常调用可变参数函数并且需要通过 va_args 进行迭代,因此迭代发生在寻找令牌的过程中,尽管可能会牺牲一些性能.然而,为了可维护性、通用性和易于实施,这似乎是目前最好的选择:
Although this solution requires some run time computation, variadic macros often times call variadic functions and requires iterating through va_args, so the iteration takes place in finding the tokens, although a bit of performance is probably sacrificed. However, for maintainability, versatility, and ease of implementation, this seems to be the best option at the moment:
#define VARIADIC_STRINGIFY(_ARGUMENTS_TO_STRINGIFY...) Variadic_Stringification_Without_Variadic_Function(#_ARGUMENTS_TO_STRINGIFY)
void Variadic_Stringification_Without_Variadic_Function (const char* _stringified_arguments)
{
strcpy(converted_arguments, _stringified_arguments);
for(char* token = strtok(converted_arguments, ","); token != 0x0; token = strtok(0x0, ","))
std::cout << token << std::endl;
}
这篇关于有没有办法在可变参数宏参数上使用 C++ 预处理器字符串化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!