为什么参数包扩展是如此有限? [英] why parameter pack expansion is so limited?

查看:99
本文介绍了为什么参数包扩展是如此有限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,为什么参数包扩展是如此有限的C ++ 11 - 是否只是监督在C ++ 11标准?为什么不可能做 bar(args)...; ?可能的解决方法比我更好吗?

I was wondering, why parameter pack expansion is so limited in C++11 - is it just oversight in C++11 standard? Why is it not possible to do just bar(args)...;? It is possible to workaround that better than I did?

#include <iostream>

int bar(int i)
{
    std::cout << i << ' ';
    return 0;
}

template <typename T>
void boo(T t)
{
    bar(t);
}

template <typename T, typename... Args>
void boo(T t, Args&&... args)
{
    bar(t);
    boo(args...);
}

template <typename... Args>
void dummy(Args&&... args)
{
}

template <typename... Args>
void foo(Args&&... args)
{
    dummy(bar(args)...);

    std::cout << std::endl;

    int dummy1[sizeof...(Args)] = { (bar(args), 0)... };

    std::cout << std::endl;

    boo(args...);
}

int main()
{
    foo(1, 3, 5);
}


推荐答案

参考提到了允许参数包展开的不同上下文。所以,一个稍微更简洁的做法是

The reference mentions the different contexts in which parameter pack expansions are allowed. So, a slightly more succinct way of doing this is

auto a = {bar(args)...};

如果bar的返回类型为void,这将不起作用。

This won't work if the return type of bar is void.

这篇关于为什么参数包扩展是如此有限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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