How to "reduce typing to create C++ types" with Uniform Initializers?
I have played a lot the new Uniform Initialization with {}
. Like this:
vector<int> x = {1,2,3,4};
map<int,string> getMap() {
return { {1,"hello"}, {2,"you"} };
}
It is undisputed that this initialization may change we program C++. But I wonder if I missed some of the magic possibilities when reading Alfonses's question in the Herb Sutter F开发者_Python百科AQs.
Alfonse: Uniform initialization (the use of {} to call constructors when the type being constructed can be deduced) has the potential to radically reduce the quantity of typing necessary to create C++ types. It's the kind of thing, like lambdas, that will change how people write C++ code. [...]
Can someone give me an example of what Alfonse exactly envisions here?
I assume he means that
std::vector<int> x{1, 2, 3, 4};
std::map<int, std::string> y{{1, "hello"}, {2, "you"}};
is significantly less typing than
std::vector<int> x;
x.push_back(1);
x.push_back(2);
x.push_back(3);
x.push_back(4);
std::map<int, std::string> y;
y.emplace(1, "hello");
y.emplace(2, "you");
Why do you discount the possibility that this was a simple mistake? That is, he wrote "create C++ types" when he meant "create C++ object". It seems strange that one wouldn't immediately think, "Oh, he meant 'object' but wrote the wrong thing."
精彩评论