1, consider a file containing int, copy to list, as follows:
Ifstream datafile ("Ints.bat");
List<int> data (istream_iterator<int> (datafile),istream_iterator<int> ());
2, the above code is not the expected behavior.
3, first from the simplest start, declaring the method int F (double d); The equivalent notation has an int f (double (d)); int f (double);
In other words, parameter names can be enclosed in parentheses, and parameter names can be omitted, leaving only the type of the formal parameter.
4, consider the int g (double (*PF) ()); The formal parameter is a method pointer, and the equivalent notation is an int g (double pf ()); We omit the parameter name and become an int g (double));
5, now consider list<int> data (istream_iterator<int> (datafile),istream_iterator<int> ()); The C + + compiler will consider this method declaration, the first parameter is: the formal parameter type is istream_iterator<int>, the parameter name is datafile, except that it is enclosed in parentheses, the second parameter is: the formal parameter type is a method pointer, The method that points to is to return ISTREAM_ITERATOR<INT>, and accepts the parameter void, omitting the parameter name.
6. There is a rule in C + +, the statement is first interpreted as a method declaration. When this explanation fails, other explanations are made. The most common are as follows:
Student s;//OK
Print (s);
Student s = Student (); Ok
Print (s);
Print (Student ()); Ok
student* s = new Student (); Ok
Print (*s);
student* s = new Student; Ok
Print (*s);
Student s (); Error
Print (s);
Because C + + will put student S (), as a method declaration.
7, how to solve the above problem?
Two approaches: one, for method invocations, arguments can be enclosed in parentheses, and for a method declaration, it is wrong to enclose the entire parameter, including the formal parameter type and the parameter name, so that it can be as follows:
List<int> Data ((istream_iterator<int> (datafile)),istream_iterator<int> ());
Method Two: Do not use anonymous objects, so that the object name, as follows:
Istream_iterator<int> begin (DataFile);
Istream_iterator<int> end;
list<int> data (begin,end);
"S6" Beware of the most annoying parsing mechanisms of the C + + compiler