How to get reference to an element of a std::tuple?
17,999
Solution 1
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.
Solution 2
get
returns a reference, rvalue reference or const reference depending on the type of its argument.
Related videos on Youtube
Author by
danijar
Researcher aiming to build intelligent machines based on concepts of the human brain. Website · Twitter · Scholar · Github
Updated on June 15, 2022Comments
-
danijar almost 2 years
You can get the value of the
n
th element of anstd::tuple
usingstd::get<n>(tuple)
. But I need to pass one element of that tuple as reference to a function.How do I get the reference to an element of a
std::tuple
?