I was thinking here about a way to get something usable /now/, rather
than for a future C++ enhancement. So I am interested in ideas to
improve the method I suggested. From the thread you posted, the idea of
using Rvalue references looks like an improvement:
#define autotie_2(v_, vt_, a1_, a2_) \
auto vt_ = v_; \
auto a1_&& = std::get<0>(vt_); \
auto a2_&& = std::get<1>(vt_)
But it is also interesting to think how it would be possible to improve
C++ in order to achieve effects like:
auto valid, res = square(x);
I think that to get that, or something similar such as "auto (valid,
res) = square(x);", would require making tuples first-class objects in
C++. Tuples would have to become part of the language, as they are in
Python, rather than a template in the library. I don't think that is a
change C++ is like to want to make.
But if C++ were to allow code like this:
T foo(void);
void bar(void) {
auto x;
...
x = foo();
}
to be treated as:
void bar(void) {
T x;
...
x = foo();
or perhaps
void bar(void) {
...
T x = foo();
}
then we would have a big step towards a good solution - and another neat
tool for other purposes:
void bar(void) {
auto x;
if (test) {
x = foo1();
} else {
x = foo2();
}
doSomething(x);
}
At the moment, code like that needs "x" to have an explicit type, or we
must use ?: rather than "if".
As far as I can see, the compiler has all the information it needs to
carry out these transformations, so it should be perfectly possible to
implement.
And this would give us:
auto valid, res;
std::tie(valid, res) = square(x);
Possibly it could be extended to:
std::tie(auto valid, auto res) = square(x);
I am not sure it would be possible to get any nicer without making
tuples part of the language.