Effective STL 6 Vexing parse

xiaoxiao2021-02-28  72

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);
转载请注明原文地址: https://www.6miu.com/read-56270.html

最新回复(0)