简体   繁体   中英

Nested Range-based for loop Not working in C++17

I have made nested Range-based for loop program in C++17.

#include <iostream>
#include <vector>

int main() 
{
    std::vector<int> v = {0, 1, 2, 3, 4, 5};

    for (int i : v) 
    {
        for (int a : i)
            std::cout << a << ' ';
    }
}

GCC genrated an error:

main.cpp: In function 'int main()':
main.cpp:10:22: error: 'begin' was not declared in this scope
         for (int a : i)

So,

  • Why does GCC generate an error for nested range based for loop?
  • What is the scope of range based for loop?
int main()  {
  std::vector<int> v = {0, 1, 2, 3, 4, 5};

  for (int i : v) {
    for (int a : i)
        std::cout << a << ' ';
  }
}

this code is nonsense and the compiler ties itself into knots trying to understand it.

The error message thus makes no sense. It is like feeding the compiler random characters, and the compoler coming back with "missing ; ".

In particular:

    for (int a : i)

where i is of type int . Thia code attempts to iterate over an int .

Types that can be iterated over are arrays, and user/library defined types with a member begin / end , and such with a non-member free function begin / end in their namespace, whose begin / end return something iterator (or pointer) like.

int is not an array nor is it user or library defined.

gcc tried to treat it as an iterable object. It isn't an array. It doesn't have a member begin. It isn't defined in a namespace containing a non-member begin . This makes gcc give up at that poimt, and output a message that it could not find the non-member begin in a nonsense location (as int has no point of definition).

This problem has nothing to do with nested loops.

int i;
for( int a:i );

will generate the same error.

This line

for (int a : i)

makes no sense. If you read the link on range-based loop you provided, you find that the inner loop would be equivalent to the following code,

{

    auto && __range = i ;
    auto __begin = begin(__range) ;
    auto __end = end(__range) ;
    for ( ; __begin != __end; ++__begin) {

        a = *__begin;
        std::cout << a << ' ';

    }

} 

The begin and end functions are useful for vectors, maps, ranges etc. because they give iterators. They are also defined by the language for arrays, where they point to the beginning and past the end of the array, so the iterating syntax is the same. They are not defined for a plain int variable.

With this information the produced given by compiler is completely clear: it refers to the absence of begin(i) in the third line of the transformed code. That it is not declared in the scope where the inner loop appears (which is: the outer loop) is just an irrelevant detail at this point, it's not defined anywhere else in the program either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM