3

I was surprised that following code compiles even produces correct result. Question is why C++11 allows dangling commas in initializer list?

auto l = {1,3,};
std::vector<int> v{ {1,3,}, };

live example

Viktor
  • 1,004
  • 1
  • 10
  • 16
  • What sort of "why?" answer are you looking for? Rationale? Or just the dry letter of the standard? – StoryTeller - Unslander Monica Apr 17 '18 at 08:12
  • Why not? What's the harm? It's unambiguous. – Jesper Juhl Apr 17 '18 at 08:12
  • Why not? Dangling commas don't cause any harm, enums accept dangling commas too. – Alan Birtles Apr 17 '18 at 08:12
  • yes, I want to understand the reason. Since for me it is ambiguous and seems like syntax error. – Viktor Apr 17 '18 at 08:14
  • Dangling commas wouldn't cause any harm only if they were consistently allowed. Right now it is a total mess. I would prefer them to be prohibited though. Reading something like `{1,3,}` makes me think that whoever wrote it forgot to write the last item. – user7860670 Apr 17 '18 at 08:16
  • 1
    IMO it makes it easier to rearrange items in the list (if you put one per line) or add new items using cut'n'paste, or using a macro. When columnated each entry has the same syntactic form. – Galik Apr 17 '18 at 08:17
  • Removing dangling commas when reorganizing stuff is annoying. This is fanservice for developers :) – Wilbert Apr 17 '18 at 08:17
  • what about `std::vector v{ {1,3}, }` ? it is constructor of vector. what constructor should be called? – Viktor Apr 17 '18 at 08:19

0 Answers0