Search code examples
c++outputstd-pairtemporarystd-tie

Outputting a Returned pair Without a Temporary


Let's say that I have a function: pair<int, int> foo() I want to directly output both elements of this without using a temporary.

Is there a way that I can output this, or maybe convert it into a string to output? Could I perhaps use tie to do this?

Here's what I'm trying to do with the temporary:

const auto temporary = foo();

cout << temporary.first << ' ' << temporary.second << endl;

Solution

  • No. You can't write that function without using a non-temporary. If you really need to, you should probably change the structure of your code. Technically, you could also use a global variable (although I strongly do not recommend this). I don't think tie would work for what you want it for either.