How to implement a function that return two integers?

Pages: 12
Why not use references if you want to return multiple variables? Simple and effective.

Perhaps because mutable references can't bind rvalues? Mutable references also cause issues when trying to use the function result to initialize constants. The second issue at least can be partially avoided by using a immediately-invoked lambda expression:

int const a = []{ int x; foo(x); return x; }();
Still, good luck using this pattern in the case where there's multiple output parameters, at least until C++17, where decomposition declarations make it possible to do something like:
int const [a, b] = []{ int x, y; foo(x, y); return std::make_pair(x, y); }();

Still, such "solutions" are ugly at best, to the point where IMO it's debatable whether the local const is even worth the trouble.
Last edited on
Topic archived. No new replies allowed.
Pages: 12