I do not understand well enough why the second call of func in the code below does not work:
#include <string> #include <iostream> #include <tuple> using Tuple = std::tuple<const int&, const std::string&>; void func(const Tuple& t) { std::cout << std::get<1u>(t) << std::endl; } int main() { const int n = 3; const std::string text = "xyz"; func(Tuple(n, text)); //Does not work: //func(Tuple(5, "abc")); return 0; } When is the temporary string "abc" destroyed?
EDIT1
So, finally, according to 康桓瑋 this works:
func(Tuple(5, std::string("abc"))); but this does not:
Tuple t(5, std::string("abc")); func(t); right? If yes what is the difference?