0

I had a code like:

std::tie(info.amount, info.price) = GetAmountAndPrice();

and the GetAmountAndPrice was returning std::pair so it worked.

But there was a code review suggesting to change the return type to struct so it's clear what the returned values represent, I think it's quite common point.

But with changing the return type from std::pair to some struct, I can't use the std::tie to set the members of info.
(And I obviously don't want to create a temporary instance of the returned struct to load the values.)

What is a neat modern way to solve this? We use C++17, but I am curious even for C++20, if it improved.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Lukas Salich
  • 959
  • 2
  • 12
  • 30
  • 1
    I think your implementation is fine as it is. However, if you want to create a custom struct an make it tie-able you should have a look at this: https://stackoverflow.com/questions/26276943/make-custom-type-tie-able-compatible-with-stdtie – jignatius Nov 23 '20 at 11:45
  • 1
    I believe that this is a direct duplicate. – Kuba hasn't forgotten Monica Nov 23 '20 at 14:32
  • Actually there can be a better option, not to make the custom type tie-able, but somehow still pass values into the caller's variables, but I won't be mad if this gets closed. – Lukas Salich Nov 23 '20 at 15:56

0 Answers0