When I write something like this:
int data[] = {10,44,56,78,8};
int i = 0;
for(int element : data)
...
that for is then translated by the compiler in a regular for? is that for
only a syntactic sugar?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The standard defines the range-based
forstatement to be equivalent to:In your case,
range-initis(data),begin-expris__range,end-expris__range + 5,for-range-declarationisint elementandstatementis.... That is, if we substitute all of these, yourforloop is equivalent to:Whether this translation is actually done by the compiler is an implementation detail. The only thing you can guarantee is that your code will be equivalent to the above code.