如何获得对std :: tuple元素的引用? [英] How to get reference to an element of a std::tuple?

查看:327
本文介绍了如何获得对std :: tuple元素的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

You can get the value of the nth element of an std::tuple using std::get<n>(tuple). But I need to pass one element of that tuple as reference to a function.

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

How do I get the reference to an element of a std::tuple?

推荐答案

返回一个引用(const或non-const),因此可以正常工作:

std::get returns a reference(either const or non-const), so this works:

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

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

演示此处.

这篇关于如何获得对std :: tuple元素的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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