gpt4 book ai didi

c++ - 如何在 C++ 包扩展中包含多行或语句?

转载 作者:行者123 更新时间:2023-12-03 06:54:27 25 4
gpt4 key购买 nike

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

#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 之前,因为不允许使用语句(在 repl.it 上得到了 [cquery] expected expression)。

更一般地说,我如何在包扩展中执行多行代码或语句之类的操作?

在工作中使用 lambda,例如

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

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

最佳答案

如果您单独声明 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;
}

关于c++ - 如何在 C++ 包扩展中包含多行或语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64163621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com