3

In the C++ code below, could somebody explain what each of these lines mean in the private section? I have tried looking it up, but I still cannot figure out what they do.

I understand that using is the equivalent to typedef in C. So:

using the_graph = graph<T_node, T_edge1, T_allocator, T_size>;

Means that you use the_graph.

But, in this instance, why would you call the scope resolution operator on it?

I don't think that it is any of the 4 methods described here.

template <class T_node, class T_edge1, class T_edge2, class T_allocator, class T_size = uint32_t>
class graph : private virtual graph<T_node, T_edge1, T_allocator, T_size>, private virtual graph<T_node, T_edge2, T_allocator, T_size>
{

public:

    using the_graph = graph<T_node, T_edge1, T_allocator, T_size>;


private:

    using typename the_graph::node_list_iterator;
    using the_graph::node_begin;
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ariane cathal
  • 85
  • 1
  • 6
  • Possible duplicate of [Where and why do I have to put the "template" and "typename" keywords?](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – Oliv Aug 16 '18 at 18:39

3 Answers3

8

The using directive is used to bring a name into the current scope that otherwise is not.

Example:

struct Foo
{
    struct Bar {};
};

int main()
{
   Bar b1; // Not ok. Bar is not in scope yet.

   using Foo::Bar;
   Bar b2; // Ok. Bar is now in scope.
}

When a name is dependent upon a template parameter, the standard requires that you use the elaborate form

using typename the_graph::node_list_iterator;

After that line, you can use node_list_iterator as a typename.

Had the_graph not been a class template, you could have used the simpler form

using the_graph::node_list_iterator;

Further reading: Where and why do I have to put the "template" and "typename" keywords?

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Those using make visible the associated name and avoid ambiguity.

Both bases should have type node_list_iterator and method(s) node_begin.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

Inside the definition and the declaration of a template, the keyword typename is used to specify that the qualified-id (ie some_name::some_member) is a type and not a variable. This keyword is necessary before template instantiation, because the compiler does not know the definition of some_name, and without typename it would supposes that some_member is a variable (or function).

Then the using directive using typename::node_list_iterator make node_list_iterator privately accessible from the graph class.

Oliv
  • 17,610
  • 1
  • 29
  • 72