Effective STL 6 Vexing parse

来源:互联网 发布:云计算优点图片 编辑:程序博客网 时间:2024/06/08 13:54

I. Warm up

int f(double (d)); // same as int f(double d);
int g(double (*pf)());  // g takes a pointer to a function as a parameter
int g(double pf()); // same as above; pf is implicitly a pointer
int g(double()); // same as above; parameter name is omitted

II. vexing parse

the function data takes two parameters

list<int>data(istream_iterator<int>(dataFile), istream_iterator<int>());

the proper way to declare data

list<int>data((istream_iterator<int>(dataFile)), istream_iterator<int>());

Note: It is not legal to surround a formal parameter declaration with parentheses, but it is legal to surround an argument to a function call with parentheses.

III. A better solution

ifstream dataFile("inst.dat");istream_iterator<int>dataBegin(dataFile);istream_iterator<int>dataEnd;list<int>data(dataBegin, dataEnd);
原创粉丝点击