我用{}玩了很多新的统一初始化.像这样:
vector<int> x = {1,2,3,4}; map<int,string> getMap() { return { {1,"hello"},{2,"you"} }; }
无可争议的是,这种初始化可能会改变我们的程序C.
但我想知道在Herb Sutter FAQs阅读Alfonses的问题时我是否错过了一些神奇的可能性.
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. […]
解决方法
我认为他的意思是
std::vector<int> x{1,4}; std::map<int,std::string> y{{1,"you"}};
打字明显少于
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");