C-style Arrays are not self-describing as it doesn't has any information about it's size, so for iterating it's elements we need size information somehow. I'm confused as how range-for loop gets the size information. It is expected to fail. Consider 2 conflicting examples,
#include <iostream>
using namespace std;
int main() {
int a[] = { 1, 2, 3, 4, 5, 6 };
for(auto x : a)
cout<<x<<" ";
cout<<endl;
return 0;
}
It ran successfully(unexpected), but
#include <iostream>
using namespace std;
void print_a(int*);
void print_a(int a[])
{
for(auto x : a)
cout<<x<<" ";
cout<<endl;
}
int main() {
int a[] = { 1, 2, 3, 4, 5, 6 };
print_a(a);
return 0;
}
This produced errors(expected).
Can someone explain as how range-for loop actually works ?
Aucun commentaire:
Enregistrer un commentaire