l***@gmail.com
2015-11-21 14:11:37 UTC
Coming from Golang and C I was saddened by the fact that standard C++ lacks
(JSON-esque) designated initializer list support.
Some pragmatic reasons to support it:
1. It seem to be some interest in this feature, e.g. 33
votes: http://stackoverflow.com/questions/18731707
2. It's makes C++ more compatible with C99.
3. Many compilers already seem have support for it. I realized it wasn't in
C++ when I turned on --pedantic.
A contrived example:
// ...
class Link {
public:
int bw;
Coord wr_c;
Coord bl_c;
int src;
int mtime;
Computer* dst;
int u;
};
// ...
int main() {
// ...
std::vector<Link> links(m);
for (int i = 0; i < m; i++) {
int a, b, c;
std::cin >> a >> b >> c;
links[i] = {c, Coord(), Coord(), a, 0, &comps[b]}; //
Non-designated
links[i] = {.src = a, .dst = &comps[b], .bw = c}; // Designated
}
// ...
}
Some (likely highly subjective) problems with the non-designated syntax:
1. Order dependent (harder to read).
2. Does not scale well, parameter ordering is increasingly difficult to
track when more fields are added.
3. Does not allow explicit initialization of a partial set of fields.
(harder to read, annoying to write)
1, 2: Understanding the designated initializer list assignment requires no
declaration or parameter order knowledge so the grammar is more context
free and therefore easier to read.
3: You are forced to write an explicit constructor or enumerate defaults
for all fields. An explicit constructor would require extra boilerplate and
introduce another independent order you have to track (more context).
Writing a constructor for a specific way you want to initialize a class
seems like code smell.
So I hope this feature would become standardized. Thank you for reading my
humble opinion.
(JSON-esque) designated initializer list support.
Some pragmatic reasons to support it:
1. It seem to be some interest in this feature, e.g. 33
votes: http://stackoverflow.com/questions/18731707
2. It's makes C++ more compatible with C99.
3. Many compilers already seem have support for it. I realized it wasn't in
C++ when I turned on --pedantic.
A contrived example:
// ...
class Link {
public:
int bw;
Coord wr_c;
Coord bl_c;
int src;
int mtime;
Computer* dst;
int u;
};
// ...
int main() {
// ...
std::vector<Link> links(m);
for (int i = 0; i < m; i++) {
int a, b, c;
std::cin >> a >> b >> c;
links[i] = {c, Coord(), Coord(), a, 0, &comps[b]}; //
Non-designated
links[i] = {.src = a, .dst = &comps[b], .bw = c}; // Designated
}
// ...
}
Some (likely highly subjective) problems with the non-designated syntax:
1. Order dependent (harder to read).
2. Does not scale well, parameter ordering is increasingly difficult to
track when more fields are added.
3. Does not allow explicit initialization of a partial set of fields.
(harder to read, annoying to write)
1, 2: Understanding the designated initializer list assignment requires no
declaration or parameter order knowledge so the grammar is more context
free and therefore easier to read.
3: You are forced to write an explicit constructor or enumerate defaults
for all fields. An explicit constructor would require extra boilerplate and
introduce another independent order you have to track (more context).
Writing a constructor for a specific way you want to initialize a class
seems like code smell.
So I hope this feature would become standardized. Thank you for reading my
humble opinion.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.