As you said, (and Fareanor said)
ttt xx,yy;
is equivalent to
ttt xx;ttt yy;
So it's not possible for
auto xx,yy = ...;
to deduce
char* xx, int yy = ...;
auto
in the way you wrote it is only able to infer one type. What you are looking for is "tuple initialization". I say it like that within quotes because it's not a real thing, but there's a common pattern in C++ and other languages that looks like what you're doing.
For example, in Python:
xx, yy = "foo", 42
Will indeed initialize xx
and yy
as you'd expect.
In C++ pre-C++17 you could kind of achieve this via tuple assignment:
char* xx;
int yy;
std::bind(xx, yy) = std::make_tuple("foo", 42);
(And C# works pretty much the same way). It's not great; you are forced to declare your types, then wrap references to them in a tuple via bind
, and finally perform assignment. Meaning you don't get to take advantage of copy elision, and your types either need to be default-constructible or else you call a constructor with meaningless parameters.
However, post C++17 we can use Structured Bindings where we can get type inference, copy elision, and all that good stuff.
Now we can say:
auto [xx, yy] = // array/tuple-like/POD-type
And we will get appropriate types for xx
and yy
. Here's an example approximating the code you posted:
struct Member{
const char* init = "foo";
int len = 42;
};
struct Test{
std::vector<Member> capture{Member{}};
};
int main()
{
Test ms;
auto [ns, nl] = ms.capture[0];
}