Syntactic sugar for std::array?

90 views
Skip to first unread message

Peet

unread,
Dec 3, 2016, 3:55:08 PM12/3/16
to ISO C++ Standard - Future Proposals
Hey,

Perhaps I am not the only one who sometimes is going to write long std::array types, I mean very long types, eg.:

std::array<std::unordered_map<uint32_t, std::vector<std::array<uint16_t, 8>>>, 8> complexArray{};

I had this thought, if it would have been easier to read if the type was written differently:

[std::unordered_map<uint32_t, std::vector<[uint16_t, 8]>>, 8] complexArray{};

The syntax [ type, size is basically syntactic sugar for std::array<type, size>.

What do you think?

~Nick

Daniel Boles

unread,
Dec 3, 2016, 4:03:48 PM12/3/16
to std-pr...@isocpp.org
As much as I hate typing, this seems like a 'slippery slope' thing to
me... why not hijack all kinds of other punctuations as shortcuts for
other types? but what if your commonly used types differ from other
people?

Plus, I feel like using square brackets is likely to make an even
tougher job for the people writing parsers nowadays, what with lambdas
and structured bindings (or whatever it's called now) floating around.

But YMMV

Nicol Bolas

unread,
Dec 3, 2016, 4:09:49 PM12/3/16
to ISO C++ Standard - Future Proposals
We already have a lot of uses for square-brackets as it is: array access, lambdas, and structured binding (depending on how the committees decided). We really don't need more.

Besides, the `std::array` part of that declaration seems to be the shortest part. This wouldn't be a problem if you created a nice alias for that map type.

Derek Ross

unread,
Dec 3, 2016, 6:47:31 PM12/3/16
to ISO C++ Standard - Future Proposals
As Nick Bolas said, usually aliases are the way to simplify complicated types. In this case you could do something like:

   
using arr_t = std::array<uint16_t, 8>;
using vecarr_t = std::vector<arr_t>;
using unomap_t = std::unordered_map<uint32_t, vecarr_t>;
std
::array<unomap_t, 8> complexArray{};

Reply all
Reply to author
Forward
0 new messages