I mean, why the following example is not a valid construction:
if (auto x = 2, y = "ab"; x != 0) {
// ...
}
(In my real use case there are calls to some functions instead of 2
and "ab"
literals.)
or
std::vector<int> cont1;
std::vector<char> cont2;
for (auto it1 = cont1.begin(), it2 = cont2.begin(); it1 != cont1.end() && it2 != cont2.end(); ++it1, ++it2) {
// ...
}
It seems that e.g. in the first example, Visual C++ deduces int
for auto
from the first 2
and then it reports: "ab"
cannot initialize int
.
But on the other side it is allowed to write:
if (auto [x, y] = std::make_pair(2, "ab"); x != 0) {
// ...
}
So here, auto
stands for different types.
Beside the answer "why", I'd like to know if there are any other workarounds (except the one I have above).