How to properly use a vector range constructor?

我的梦境 提交于 2019-12-01 05:11:10

You have fallen victim to the Most Vexing Parse, where the compiler sees your declaration as a function strings returning a vector<string>, taking two arguments:

  • an istream_iterator<string> called file
  • an unnamed pointer to function taking no arguments and returning a istream_iterator<string>.

To eliminate the vexing parse, use an extra pair of parentheses around the first argument:

vector<string> strings((istream_iterator<string>(file)) , istream_iterator<string>());
//                     ^                              ^

or, alternatively in C++11, use curly braces for the strings constructor

vector<string> strings{istream_iterator<string>(file) , istream_iterator<string>()};
//                    ^                                                           ^

NOTE: Clang warns you about it through -Wvexing-parse (on by default).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!