How to get a link to the std :: tuple element?

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

How to get a link to an item std::tuple?

+5
source share
3 answers

std::get returns a link (either const or not 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 is here .

+8
source

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

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

+1
source

get , rvalue .

0

All Articles