Nested structure in a template class with std :: map :: const_iterator?

The following code generates a syntax error in the line where the iterator is declared:

template <typename T>
class A
{
  public:

    struct B
    {
       int x, y, z;
    };

    void a()
    {
        std::map<int, B>::const_iterator itr; // error: ; expected before itr
    }

    std::vector<T> v;
    std::map<int, B> m;
};

This only happens when A is a pattern. What is wrong with this code? If I move B from A, the code compiles fine.

+3
source share
1 answer

You need a type name:

 typename std::map<int, B>::const_iterator itr;

An iterator is a dependent type (B dependent), and when you have this situation, the compiler requires you to specify it using the type name.

There is a reasonable discussion of the problem here .

+8
source

Source: https://habr.com/ru/post/1705009/


All Articles