简体   繁体   中英

C++ 'using' and 'using typename' in a header file

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;
};

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?

Those using make visible the associated name and avoid ambiguity.

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

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.

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