你可以使用带逗号的auto吗

Can you use auto with a comma

能否使用 auto 推导类型,其中有一个逗号来指示两个或多个变量的初始化。像这样:

 auto p = c.begin(), e = c.end();

或者两个初始化的存在(可能)对编译器来说太混乱了? C++ 标准允许什么?

是的,只要它们属于同一类型(在您的示例中就是这种类型),您就可以这样初始化它们。

Can you use an auto deduced type where you have a comma operator to indicate initialization of two or more variables.

是的。

Or is the presence of two initialization too confusing for a compiler?

不,不是。只要自动推导不会导致类型不一致,在同一语句中使用 auto 声明多个变量就可以了。

来自C++11 Standard/7.1.6.4 auto specifier/3

auto x = 5;                 // OK: x has type int
const auto *v = &x, u = 6;  // OK: v has type const int*, u has type const int

但是,您不能使用:

const auto *v = &x, u = 6.0;

并希望v的类型推导为const int*u的类型推导为const double