"S6" Beware of the most annoying parsing mechanisms of the C + + compiler

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.