gpt4 book ai didi

c++ - 如何获取对 std::tuple 元素的引用?

转载 作者:行者123 更新时间:2023-12-01 21:30:44 37 4
gpt4 key购买 nike

可以获取n的值std::tuple 的第一个元素使用std::get<n>(tuple) 。但我需要将该元组的一个元素作为对函数的引用传递。

如何获取 std::tuple 的元素的引用?

最佳答案

std::get返回一个引用(const 或非 const),所以这是有效的:

void fun(int &a) {
a = 15;
}

void test() {
std::tuple<int, char> foo{ 12, 'a' };
fun(std::get<0>(foo));
}

演示 here .

关于c++ - 如何获取对 std::tuple 元素的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15835762/

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