繁体   English   中英

c ++组合来自两个(或更多)参数包的值

[英]c++ combine values from two (or more) parameter packs

如何在C ++中组合两个参数包的值? 换句话说,如何编写函数

LetsStart<int, -1, 10, 3>("This is -1", "And this is 10", "3!!!");

哪个会输出

-1 This is -1                                                                                                                                   
10 And this is 10                                                                                                                               
3 3!!! 

即它应该从第一个包中选择第一个值,从第二个包中选择第一个值,然后从两个包中选择第二个值,然后从两个包中选择第三个值,依此类推......

在我尝试的第一次尝试这样的事情

template<class T, T... Firsts, class TSecond>
void LetsStart(TSecond... Seconds) {
   Output((Firsts, Seconds)...);
}

但这不起作用......

//当然我写了这个函数 ,但我相信有更多正确而简单的方法来完成这个任务。 所以,你能指出我的方式吗?

在C ++ 11中,您可以使用以下函数:

template<typename T, T... V, typename F, typename... A>
void f(F &&f, A&&... args) {
    static_assert(sizeof...(V) == sizeof...(A), "Argument lists size mismatch");
    int arr[] = { 0, (std::forward<F>(f)(V, std::forward<A>(args)), 0)... };
    (void)arr;
}

它接受两个请求的参数列表和一个额外的函数F ,然后它解压缩参数并将每个列表的第N个传递给给定的函数。
如果没有提供参数,它也应该工作。

在C ++ 17中,您可以按照以下方式重写它:

template<typename T, T... V, typename F, typename... A>
void f(F &&f, A&&... args) {
    static_assert(sizeof...(V) == sizeof...(A));
    (std::forward<F>(f)(V, std::forward<A>(args)), ...);
}

在这两种情况下,我都检查过两个列表的大小是否相同,因为在这种情况下没有指定要做什么。

您可以使用下面的示例main测试这两个函数:

#include<utility>
#include<iostream>

// ...

int main() {
    auto l = [](auto v, auto s) { std::cout << v << " " << s << std::endl; };
    f<int, -1, 10, 3>(l, "This is -1", "And this is 10", "3!!!");
}

C ++ 11版本:

CE: https//gcc.godbolt.org/z/xIUL1J

#include<iostream>

template<class... T>
void LetsStart(){}

template<class T, class U>
void print(T t, U u)
{
    std::cout << t << " " << u << '\n';
}

template<class T, T f,  T... ts, class A, class... Arg>
void LetsStart(A&& a, Arg&&... args){
    print(f, a);
    LetsStart<T, ts...>(args...);
}

void foo() {
    LetsStart<int, -1, 10, 3>("This is -1", "And this is 10", "3!!!");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM