3

假设我有一些像下面这样的简单代码,它只打印一个元组中的所有值并跟踪当前迭代。

#include <iostream>
#include <tuple>
#include <utility>

using std::cout;

int main() {
    std::tuple<int, double, size_t, unsigned, short, long long, long> my_tuple(7, 4, 1, 8, 5, 2, 9);
    //Can you spot the pattern? :)
    std::apply(
        [](auto&&... current_value) {
            size_t i = 0; //This is only executed once
            ((
                cout << i++ << ", " << current_value << "\n" //This is repeated the length of the tuple
            ), ...);
        }, my_tuple
    );
    return 0;
}

例如,如果我想仅在索引大于 2 时打印元组值,我该怎么做?我不能简单地if在因为语句之前放一个cout不允许([cquery] expected expression在 repl.it 上)。

更一般地说,我怎样才能在包扩展中执行多行代码或语句之类的事情?

在内部使用 lambda 可以工作,例如

std::apply(
    [](auto&&... current_value) {
        size_t i = 0;
        ((
            [&current_value, &i](){
                cout << i << ", " << current_value << "\n";
                ++i;
            }()
        ), ...);
    }, my_tuple
);

但我无法想象这是最有效(或预期)的解决方案。

4

1 回答 1

2

如果你单独声明 lambda 然后折叠调用它,它会更好读:

auto f = [&](auto& value){
    cout << i << ", " << value << "\n";
    ++i;
};
(f(current_value), ...);

您还可以使用 Boost.Mp11 的东西tuple_for_each来避免间接层:

size_t i = 0;
tuple_for_each(my_tuple, [&](auto& value){
    cout << i << ", " << value << "\n";
    ++i;
});

这是一种比经历更直接的方式来做这样的事情std::apply。即使您不想使用 Boost.Mp11(并且您应该想要),这也很容易实现。


有一个名为扩展语句的语言提案,这将使其成为一流的语言功能。它并没有完全制作 C++20,但可能是 C++23:

size_t i = 0;
template for (auto& value : my_tuple) {
    cout << i << ", " << value << "\n";
    ++i;
}
于 2020-10-02T15:25:18.677 回答