有没有办法使用C ++ preprocessor字符串化的可变参数宏参数的方法吗? [英] Is there a way to use C++ preprocessor stringification on variadic macro arguments?

查看:120
本文介绍了有没有办法使用C ++ preprocessor字符串化的可变参数宏参数的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的猜测是这个问题的答案是否定的,但如果有办法将是真棒。为了澄清,假设我有以下宏:

 的#define MY_VARIADIC_MACRO(X ...)//做一些事情在这里的宏定义

我想要做的是不知它传递给一个可变参数函数之前,X的所有变量进行字符串化;这里的关键词是前。我知道有没有办法真正从宏定义内访问各个参数,但有一种方法来字符串化所有参数,与可能像下面?

 的#define MY_VARIADIC_MACRO(X ...)some_variadic_function(一些字符串,#X)


解决方案

好吧,我不是故意要在这里回答我的问题,但我已经拿出一个像样的解决方案,是马克·威尔金斯的组合有点回答和例子,我在这个问题了。

这是可能的字符串化的整套可变参数集,然后包含字符串中的逗号分隔。这里有一个简单的例子:

 的#define MY_VARIADIC_MACRO(X ...)的printf(#X)

使用上面的宏向您传递给整个宏观组参数的获取字符串化。

然后你可以再定义一个函数使用可变参数宏来标记使用分隔逗号,从而获得组记号化字符串这些参数:

 的#define MY_VARIADIC_MACRO(X ...)tokenize_my_arguments(#X)

再有实际上不再是具有可变参数宏调用一个可变参数函数,我可以通过我的常数C字符串数组迭代很好,而不是通过在va_arg迭代的依赖关系。

的*从编辑新东西遵循的 *

每Tim的评论,这里的解决方案的详细信息。因为它是在匆忙做,我不得不从端口什么我正在请原谅任何错误。此外,它并不意味着是复制/粘贴解决方案,因为它只输出的论据来证明POC的字符串化,而应该是足以证明功能。

虽然这种解决方案需要一定的运行时间计算,复杂的宏经常时代呼唤可变参数的功能,并需要通过迭代va_args,所以迭代发生在发现的记号,虽然性能有些可能是牺牲。然而,可维护性,灵活性和易于实施的,这似乎是目前最佳的选择:

 的#define VARIADIC_STRINGIFY(_ARGUMENTS_TO_STRINGIFY ...)Variadic_Stringification_Without_Variadic_Function(#_ ARGUMENTS_TO_STRINGIFY)无效Variadic_Stringification_Without_Variadic_Function(为const char * _stringified_arguments)
{
    的strcpy(converted_arguments,_stringified_arguments);    对于(字符*令牌= strtok的(converted_arguments,);记号=为0x0;!令牌= strtok的(为0x0,))
        性病::法院LT&;<令牌LT;<的std :: ENDL;
}

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

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)

解决方案

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)

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 *

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.

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 ++ preprocessor字符串化的可变参数宏参数的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆