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

限于喜欢 提交于 2019-12-01 04:21:58

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));
}

Demo here.

get returns a reference, rvalue reference or const reference depending on the type of its argument.

std::get returns a reference to the element at the specified position in the tuple.

http://www.cplusplus.com/reference/tuple/get/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!