c – 可以使用模板预处理器hackery来支持参数列表中间的变量参数吗?

c – 可以使用模板预处理器hackery来支持参数列表中间的变量参数吗?,第1张

概述我偶然发现了看起来像这样的旧代码: void dothing(bool testBool, const std::string& testString1, const std::string& file, int line, const std::string& defaultS 我偶然发现了看起来像这样的旧代码:
voID dothing(bool testBool,const std::string& testString1,const std::string& file,int line,const std::string& defaultString = ""){     // do something...}voID dothings(bool testBool,const std::string& testString2,const std::string& defaultString = ""){    dothing(testBool,testString1,file,line,defaultString);    dothing(testBool,testString2,defaultString);}voID dothings(bool testBool,const std::string& testString3,const std::string& defaultString = ""){   dothings(testBool,defaultString);   dothing(testBool,testString3,const std::string& testString4,defaultString);}

这很荒谬,我试图将其重构为:

voID dothings(bool testBool,std::initializer_List<std::string> testStrings,const std::string& defaultString = ""){    for(auto iter = testStrings.begin(); iter != testStrings.end(); ++iter)    {        dothing(testBool,*iter,defaultString);    }}

问题是这些函数被大量使用,我想编写一个宏或模板,以便所有以前的函数构造所有测试字符串的字符串的初始化列表,并将它们传递给一个新函数.我想写这样的东西:

#define dothings(testBool,(args),line) dothings(testBool,{args},line)

我并不关心这些函数中的默认字符串,但是如果有一种方法可以支持它,那就太棒了.

我可以访问c 11编译器并仅提升.

我无法重新排序这些函数的参数.

我看过一些关于变量参数宏的有趣帖子,但它并没有点击如何将它们应用于这种情况.

解决方法 这只是可能的解决方案之一,可以改进以检测最后是否存在额外的默认字符串(通过一些其他元编程技术与SFINAE一起).这个利用 indices trick将参数列表分成两个子序列:一个用于三个尾随参数,一个用于字符串本身.最终,每个字符串与其余参数配对,对函数dothing的调用是 expanded.
voID dothing(bool testBool,const std::string& str,const std::string& defaultString){    // processing of a single str}template <typename... Args,std::size_t... Is>voID dothings(bool testBool,std::index_sequence<Is...>,Args&&... args){    auto tuple = std::make_tuple(std::forward<Args>(args)...);    using expander = int[];    static_cast<voID>(expander{ 0,(dothing(testBool,std::get<Is>(tuple),std::get<sizeof...(Args)-3>(tuple),std::get<sizeof...(Args)-2>(tuple),std::get<sizeof...(Args)-1>(tuple)),0)... }); }template <typename... Args>voID dothings(bool testBool,Args&&... args){    dothings(testBool,std::make_index_sequence<sizeof...(Args)-3>{},std::forward<Args>(args)...); }

DEMO

总结

以上是内存溢出为你收集整理的c – 可以使用模板/预处理器hackery来支持参数列表中间的变量参数吗?全部内容,希望文章能够帮你解决c – 可以使用模板/预处理器hackery来支持参数列表中间的变量参数吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1238719.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存