微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我偶然发现了看起来像这样的旧代码
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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐