-2

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

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

Or is the presence of two initializations (potentially) too confusing for a compiler? What does the C++ standard allow?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • 6
    did you try it? – pm100 Dec 08 '17 at 17:25
  • `for (auto p = c.begin(), e = c.end(); p != e; ++p);` works just fine – Killzone Kid Dec 08 '17 at 17:26
  • 2
    `auto [p, e] = std::make_pair(c.begin(), c.end());` :-) – Jarod42 Dec 08 '17 at 17:32
  • @Jarod42 `auto[p, e] = std::pair(c.begin(), c.end())` :) – Rakete1111 Dec 08 '17 at 17:36
  • 1
    @pm100 That is not how questions work here. Trying something might tell you what a particular compiler allows, not what the language allows. – Raedwald Dec 08 '17 at 17:51
  • 1
    @Raedwald you asked if it would be too confusing for a compiler. If you tried it yourself you might find that your compiler is not confused. – Kevin Dec 08 '17 at 17:52
  • 1
    @pm100 https://meta.stackexchange.com/a/172760/170084 My question is not a no-effort noob "debug me" or "do my homework" question, but a question about how two language facilities are permitted to interact. – Raedwald Dec 08 '17 at 18:03
  • @Raedwald 100% agree, an experiment with a particular compiler proves nothing. I was just asking if he tried it. He might have said - well it worked on gcc xxx, but vs yyy barfed – pm100 Dec 08 '17 at 18:17

2 Answers2

3

Yes you can initialize them that way as long as they are of the same type (which they are in your example).

Justin Randall
  • 2,243
  • 2
  • 16
  • 23
  • 2
    Declaring unrelated entities in the same line will indeed adversely impact readability. But declaring *closely related* entities in a single declaration (and even in one line) should be prefectly fine. (E.g. "begin" and "end" iterators.) I'd say that it even *improves* readability. Structured binding declaration in C++17 are actually step in that direction, as are many other language features. (Not my downvote, BTW) – AnT stands with Russia Dec 08 '17 at 17:29
  • Ill-advised recommendation turned +1 into -1. `auto b = v.begin(), e = v.end()` is an idiomatic way. – SergeyA Dec 08 '17 at 17:41
  • 2
    Fair points I took out my personal opinion as it got in the way of the correct answer. – Justin Randall Dec 08 '17 at 17:55
2

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

Yes.

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

No, it is not. As long as the auto deduction does not result in inconsistent types declaring multiple variables in the same statement using auto is fine.

From the 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

However, you may not use:

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

and hope for the type of v to be deduced as const int* and the type of u to be deduced as const double.

R Sahu
  • 204,454
  • 14
  • 159
  • 270