Am 09.03.23 um 10:13 schrieb
Mut...@dastardlyhq.com:
I think you don't understand the problem. The problem is not matching
the pair with the auto in your catch phrase. The problem is how to
compile the code within the catch phrase. So, say, you do:
catch(auto &[str,val])
{
std::cout<<str.size();
}
What is the compiler supposed to do with the line that calls size() on
the object str? In regular code, the compiler knows, e.g. that str is of
type std::string. It then looks up the address of the function
std::string::size() and inserts a call to that address. But "str" could
be, despite its name, as well a std::vector. Then the call would need to
go to std::vector::size(), otherwise you get illegal memory thrashing.
The only way to implement something like that would be an OO system
where all the objects derive from a common base, like it is in Java,
and, at runtime, lookup the name of the member function in a jump table.
That means, OTOH, that the code in the catch would be compiled in a
completely different way than it is elswehere in C++, and run much
slower as well. Even a simple integer addition would need to go through
this kind of thing, because "a+b" could as well be string concatenation
(call to std::string::operator +) as well as a simple integer addition,
which compiles to a single machine instruction. This is the reason why
it can be implemented in, say, Python, where all code goes through an
interpreter loop, whereas in C++, it usually doesn't.
Christian