我需要一种从另一个元组生成新元组的方法。
std::string f1(int a)
{
std::string b = "hello";
return b;
}
float f2(std::string a)
{
float b = 2.5f;
return b;
}
int f3(float a)
{
int b = 4;
return b;
}
int main()
{
auto t1 = std::make_tuple(1, "a", 1.5f);
//New tuple ---> std::tuple<std::string, float, int>(f1(1), f2("a"), f3(1.5));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这只是我想做的一个最小的例子。有没有办法在 C++20 中做到这一点,也许使用std::tuple_cat?
您可以使用std::apply以下方法来执行此操作:
template<class Tuple, class... Fns>
auto tuple_transform(const Tuple& t, Fns... fns) {
return std::apply([&](const auto&... args) {
return std::tuple(fns(args)...);
}, t);
}
auto t1 = std::make_tuple(1, "a", 1.5f);
auto t2 = tuple_transform(t1, f1, f2, f3);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
314 次 |
| 最近记录: |